Reputation: 6129
Either I'm very tired or something weird is happening that I'm not aware of, because the code below is resulting in undefined symbols for Foo::A and Foo::B when linking. This is minimized as much as I could from a larger project, but shows the essence of what I'm looking at.
#include <algorithm>
struct Foo
{
static const int A = 1;
static const int B = 2;
};
int main()
{
return std::min(Foo::A, Foo::B);
}
Without the std::min function template it works fine, i.e. just return Foo::A. Also fine is when defining the static ints outside a class/struct (global in this simple case). However, as soon as they're inside like this, the linker cannot find them.
Can someone explain what's happening?
Upvotes: 33
Views: 12893
Reputation: 2027
Another solution would be to inline
your static
variables, like this it will be available in the final translation unit which does away with the Undefined Symbols error.
Note this will only work in post C++11 AFAIK.
Upvotes: 0
Reputation: 34423
The code you have provided is non-standard. While you can provide initializers for const static int members directly in the class, you still need to provide separate definitions. It is weird, a kind of unexpected, but you are expected to write it like this:
#include <algorithm>
struct Foo
{
static const int A = 1;
static const int B = 2;
};
const int Foo::A;
const int Foo::B;
int main()
{
return std::min(Foo::A, Foo::B);
}
Quote from standard can be found in a similar question at const and static specifiers in c++
As for why you can often get around even without providing the definition: if you are using those members only in constant expressions, compiler will always resolve them directly and there will be no access left for linker resolution. It is only when you use it in some way which cannot be handled by compiler directly, and only in such case the linker will detect the symbol is undefined. I guess this is probably a bug in the Visual Studio compiler, but given the nature of the bug I doubt it will be ever fixed.
Why your source falls into the "linker" category is something I do not see, one would need to dissect the std::min to understand that. Note: When I have tried it online with GCC, it worked, the error was not detected.
Another alternative is to use enum. This version can also come handy when you hit an old compiler which does not support static const int "inline" initializers (such as was Visual Studio 6). Note however that with std::min you are hitting other problems with enums and you need to use an explicit instantiation or casting, or have both A and B in one named enum as in the answer by Nawaz:
struct Foo
{
enum {A = 1};
enum {B = 2};
};
int main()
{
return std::min<int>(Foo::A, Foo::B);
}
Note: even Stroustrup C++ FAQ gets this wrong and does not require the definition as strictly as the standard does:
You can take the address of a static member if (and only if) it has an out-of-class definition
The definition is required by a standard in 9.4.2:
C++03 wording:
The member shall still be defined in a name-space scope if it is used in the program and the namespace scope definition shall not contain an initializer
C++11 wording of 9.4.2 is a bit different:
3 The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program
3.2 says following about odr-use:
3 A variable x whose name appears as a potentially-evaluated expression ex is odr-used unless x is an object that satisfies the requirements for appearing in a constant expression (5.19) and ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e, or e is a discarded-value expression (Clause 5).
4 Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required.
I have to admit I am not sure what the exact implications of C++11 wording are, as I fail to understand the odr-use rules.
Upvotes: 45
Reputation: 51
There are good answers here, but one additional thing to note is that the parameters of std::min()
are references, which is what requires the addresses of the passed in variables, and since those variables don't make it to the object file for the compilation unit, the linker cannot resolve their addresses.
You are probably getting this in an non-optimized build, correct?
I bet that you won't get this with gcc if you enable optimizations. The call to std::min()
will get inlined and the references will go away.
Also, if you were to assign Foo::A
and Foo::B
to two local variables right before the call to std::min()
, this issue would also go away.
This isn't ideal, but if you don't own the code that defines the variables that are causing you this issue, then this is something you can consider.
Upvotes: 2
Reputation: 4514
Seeing as you're basically using struct as a namespace, why not just use namespace:
#include <algorithm>
namespace Foo
{
const int A = 1;
const int B = 2;
};
int main()
{
return std::min(Foo::A, Foo::B);
}
Upvotes: 1
Reputation: 361672
If you just want integral values,then you can define enum
as well:
#include <algorithm>
struct Foo
{
enum integrals { A = 1, B = 2} ;
};
int main()
{
return std::min(Foo::A, Foo::B);
}
This is more than enough. No declaration needed outside the class!
Online Demo : http://www.ideone.com/oE9b5
Upvotes: 3
Reputation: 58685
You must define the static constants outside the class definition.
struct Foo {
static const int A;
static const int B;
};
const int Foo::A = 1;
const int Foo::B = 2;
Upvotes: 2