Linker error with static data member in templated class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I am using gcc 4.4.5 with the c++0x extensions turned on and am having some issues defining static variables inside partially specialised template classes. I've put together some fairly straight-forward demonstration code as follows:

struct C1
{
C1() : i(0)
 { printf("default constructor\n"); }

C1(const C1& other) = delete;

/*C1(const C1& other) : i(other.i)
 { printf("copy constructor\n"); }*/

int i;
};

struct C2
{
static C1 op;
};

template <typename T>
struct C3
{
static C1 op;
};

template <typename T>
struct C4
{
static C1 op;
};

// Initialise C2::op
C1 C2::op;

// Initialise C3::op for type int
template<> C1 C3<int>::op;

// Initialise C4::op for type int
template<> C1 C4<int>::op = C1();


int main()
{
printf("%i\n", C2::op.i);
printf("%i\n", C3<int>::op.i);
printf("%i\n", C4<int>::op.i);
return 0;
}


Like this the code fails to compile with the error "deleted function C1::C1(const C1&) used here" referring to the line that initialises C4<int>::op since it appears to need the copy constructor. I say "appears" since if I give C1 a copy constructor, then I find when running the code that it doesn't actually call the copy constructor.

However, if the compiler error is fixed, then a separate linker error occurs: "undefined reference to 'C3<int>::op'". It therefore seems that it is not possible, at least by this syntax, to default construct C3<int>::op. This is in contrast to the default construction of C2::op which is possible.

Is there another syntax by which I can default construct C3<int>::op? - since the implementation I will need of C1 requires that the copy constructor be disabled and so the syntax I use with C4<int>::op is not going to be possible.

Thanks!

Andy



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux