
Hi all, I am currently having problem with a static const class member (in a header only project): // type.hpp template< typename Property > struct property_base { typedef Property type; }; struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); }; // main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); } This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ... If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help? Regards, Olivier

AMDG Olivier Tournaire wrote:
I am currently having problem with a static const class member (in a header only project):
// type.hpp template< typename Property > struct property_base { typedef Property type; };
struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); };
// main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); }
This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ...
If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help?
Putting the definition in a header can indeed cause a multiple definition error. The definition needs to be in a cpp file. In Christ, Steven Watanabe

Thank you Steven,
2010/3/13 Steven Watanabe
AMDG
Olivier Tournaire wrote:
I am currently having problem with a static const class member (in a header only project):
// type.hpp template< typename Property > struct property_base { typedef Property type; };
struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); };
// main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); }
This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ...
If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help?
Putting the definition in a header can indeed cause a multiple definition error. The definition needs to be in a cpp file.
Is there an other way to do that? I want the project to stay header only so I cannot introduce a cpp file. Are they some metaprogramming techniques that could lead to the same behavior? Regards, Olivier
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Sat, 13 Mar 2010 09:43:18 +0100
Olivier Tournaire
Thank you Steven,
2010/3/13 Steven Watanabe
AMDG
Olivier Tournaire wrote:
I am currently having problem with a static const class member (in a header only project):
// type.hpp template< typename Property > struct property_base { typedef Property type; };
struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); };
// main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); }
This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ...
If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help?
Putting the definition in a header can indeed cause a multiple definition error. The definition needs to be in a cpp file.
Is there an other way to do that? I want the project to stay header only so I cannot introduce a cpp file. Are they some metaprogramming techniques that could lead to the same behavior?
Is "extern" what you are looking for? extern cons some_type::type some_type::first_value; Cheers, -- Manfred

On Sat, 13 Mar 2010 09:43:18 +0100
Olivier Tournaire
Thank you Steven,
2010/3/13 Steven Watanabe
AMDG
Olivier Tournaire wrote:
I am currently having problem with a static const class member (in a header only project):
// type.hpp template< typename Property > struct property_base { typedef Property type; };
struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); };
// main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); }
This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ...
If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help?
Putting the definition in a header can indeed cause a multiple definition error. The definition needs to be in a cpp file.
Is there an other way to do that? I want the project to stay header only so I cannot introduce a cpp file. Are they some metaprogramming techniques that could lead to the same behavior?
Is "extern" what you are looking for? extern const some_type::type some_type::first_value; Cheers, -- Manfred

2010/3/13 Manfred Doudar
On Sat, 13 Mar 2010 09:43:18 +0100 Olivier Tournaire
wrote: Thank you Steven,
2010/3/13 Steven Watanabe
AMDG
Olivier Tournaire wrote:
I am currently having problem with a static const class member (in a header only project):
// type.hpp template< typename Property > struct property_base { typedef Property type; };
struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); };
// main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); }
This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ...
If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help?
Putting the definition in a header can indeed cause a multiple definition error. The definition needs to be in a cpp file.
Is there an other way to do that? I want the project to stay header only so I cannot introduce a cpp file. Are they some metaprogramming techniques that could lead to the same behavior?
Is "extern" what you are looking for?
Maybe. However, I am not sure. I still have to provide a definition of the member somewhere. Where should I do this (in a header only project ...)?
extern const some_type::type some_type::first_value;
Cheers, -- Manfred
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Sat, 13 Mar 2010 19:58:00 +0100
Olivier Tournaire
2010/3/13 Manfred Doudar
On Sat, 13 Mar 2010 09:43:18 +0100 Olivier Tournaire
wrote: Thank you Steven,
2010/3/13 Steven Watanabe
AMDG
Olivier Tournaire wrote:
I am currently having problem with a static const class member (in a header only project):
// type.hpp template< typename Property > struct property_base { typedef Property type; };
struct some_type : property_base< uint32_t > { BOOST_STATIC_CONSTANT( type, first_value = 1 ); BOOST_STATIC_CONSTANT( type, second_value = 2 ); };
// main.cpp #include "type.hpp" ... int main(int argc, char** argv) { BOOST_CHECK_EQUAL( 1 , some_type::first_value ); }
This produces a link error (g++ 4.3.4): undefined reference to some_type::first_value. Everything is fine with visual ...
If I add const some_type::type some_type::first_value; after the struct definition, it compiles and links fine. However, would it produce a multiple definition link error in some cases? Could you please help?
Putting the definition in a header can indeed cause a multiple definition error. The definition needs to be in a cpp file.
Is there an other way to do that? I want the project to stay header only so I cannot introduce a cpp file. Are they some metaprogramming techniques that could lead to the same behavior?
Is "extern" what you are looking for?
Maybe. However, I am not sure. I still have to provide a definition of the member somewhere. Where should I do this (in a header only project ...)?
extern const some_type::type some_type::first_value;
If you add the line above (extern ...) just after your struct some_type in type.h, works for me. Cheers, -- Manfred
participants (3)
-
Manfred Doudar
-
Olivier Tournaire
-
Steven Watanabe