“static const int” causes linking error (undefined

2020-03-10 05:33发布

I am baffled by the linker error when using the following code:

// static_const.cpp -- complete code
#include <vector>

struct Elem {
    static const int value = 0;
};

int main(int argc, char *argv[]) {
    std::vector<Elem> v(1);
    std::vector<Elem>::iterator it;

    it = v.begin();
    return it->value;
}

However, this fails when linking -- somehow it needs to have a symbol for the static const "value."

$ g++ static_const.cpp 
/tmp/ccZTyfe7.o: In function `main':
static_const.cpp:(.text+0x8e): undefined reference to `Elem::value'
collect2: ld returned 1 exit status

BTW, this compiles fine with -O1 or better; but it still fails for more complicated cases. I am using gcc version 4.4.4 20100726 (Red Hat 4.4.4-13).

Any ideas what might be wrong with my code?

5条回答
劳资没心,怎么记你
2楼-- · 2020-03-10 05:46

Why not just do this?

return Elem::value;

But the answer is that you are assigning a value in the declaration. This is supposed to work for basic types such as int, and is only required for complex types (i.e. classes, such as if you had a string instead of int). What I have found in practice is that this is hit or miss depending on what version of what compiler you are using. And, as you found out, which optimization level.

查看更多
放荡不羁爱自由
3楼-- · 2020-03-10 05:48

Try writing it as

struct Elem {
    static const int value;
};

const int Elem::value = 0;

etc

.

查看更多
混吃等死
4楼-- · 2020-03-10 05:54

static class members are generally supposed to be defined outside the class (declared inside, defined outside) in one compilation unit.

I don't remember how that interacts with inline initialization of const static integral members.

查看更多
可以哭但决不认输i
5楼-- · 2020-03-10 05:55

If you want to initialize it inside the struct, you can do it too:

struct Elem {
    static const int value = 0;
};

const int Elem::value;
查看更多
相关推荐>>
6楼-- · 2020-03-10 06:02

Also see this post: essentially, the problem is that somehow compiler ends up expanding your code into taking the address of Elem::value.

查看更多
登录 后发表回答