[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Linker problem with template specialisation
From: |
Thomas Maeder |
Subject: |
Re: Linker problem with template specialisation |
Date: |
Thu, 29 May 2008 17:51:50 +0200 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux) |
tsotsi@gmail.com writes:
> enum foo
> {
> null_foo,
> foo1,
> foo2
> };
>
> template<typename T>
> struct data
> {
> T val;
> bool set;
>
> template<typename U>
> data<T>& operator=(const U &that)
> {
> val = that;
> set = true;
> return *this;
> }
> };
>
> struct foo_1 { };
> struct foo_2 { };
>
> template<typename T> struct map_foo_type { static foo const id =
> null_foo; }; // worst match
> template<> struct map_foo_type<foo_1> { static foo const id = foo1; };
> template<> struct map_foo_type<foo_2> { static foo const id = foo2; };
[NB: The above two lines only contain declarations of the static data
members called id. I don't see the corresponding definitions in the
code you posted. E.g.:
template<typename T>
foo const map_foo_type<T>::id;
foo const map_foo_type<foo_1>::id;]
> Using the above code, I want to be able to do the following:
>
> data<int> mid = map_foo_type<foo1>::id;
>
> When I compile with gcc 3.3.6 I get a linker error - undefined symbol
> mep_foo_type<foo1>::id
Funny, I get
test.cpp:50: error: type/value mismatch at argument 1 in template parameter
list for 'template<class T> struct map_foo_type'
test.cpp:50: error: expected a type, got 'foo1'
Did you meen foo_1 when you wrote foo1?
If I perform that change, I get
test.cpp:50: error: conversion from 'const foo' to non-scalar type 'data<int>'
requested
because data<int> doesn't have a constructor that takes a foo const.
> If I have an intermediate step:
>
> foo id = map_foo_type<foo1>::id
> data<int> mid = id;
>
> then it links fine.
Hmm, this surely doesn't compile without a ';? at the end of the first
line and when the two lines are at global scope; is this code in a
function? If I move the fixed lines into a function, I still get the
error mentioned above (test.cpp:50).
> Alternately, if I change operator= above to take const U data, rather
> than a reference to const U, it also links fine!
I fail to see how the data assignment operator could play a role;
there is no assignment to a data<T> object in the entire code you posted.
To initialize objects, C++ uses constructors, not assignment operators.
> What is the problem please?
Primarily that you don't post the code that you have tested. :-)