Template args and static const members

N

Nick Battle

I was surprised to find the code below won't link, though both
compilers I have (g++ and aCC) give the same result so it's presumably
not a bug. Is this the problem, I've read elsewhere, about template
args having to have external linkage? But then why does the commented
out inline version work? Any help appreciated...

template <typename T> const T &max(const T &a, const T &b)
{
return (a > b ? a : b);
}

class C
{
public:
C(void); // THIS WORKS { int x = max(456, value); }

static const int value = 123;
};

C::C(void) { int x = max(456, value); } // THIS FAILS

int main(int, char *[]) { }


The linkage error is that it fails to resolve C::value.
 
S

Sumit Rajan

Nick Battle said:
I was surprised to find the code below won't link, though both
compilers I have (g++ and aCC) give the same result so it's presumably
not a bug. Is this the problem, I've read elsewhere, about template
args having to have external linkage? But then why does the commented
out inline version work? Any help appreciated...

template <typename T> const T &max(const T &a, const T &b)
{
return (a > b ? a : b);
}

class C
{
public:
C(void); // THIS WORKS { int x = max(456, value); }

static const int value = 123;
};

You 'll need to provide a definition here:
const int C::value;

C::C(void) { int x = max(456, value); } // THIS FAILS

int main(int, char *[]) { }


The linkage error is that it fails to resolve C::value.
 
N

Nick Battle

You 'll need to provide a definition here:
const int C::value;

That works, yes. Thanks.

But why was it happy with the inline constructor's use of the value?
 
P

paulius-maruska

I just tryed the code bellow on VC++ - it works like a charm.
Code:
#include <iostream>

[QUOTE="template"]
b ? a : b); }[/QUOTE]

class A
{
public:
	A(void);
	static const int value = 123;
};
A::A(void) { int x = max(456, value); std::cout << "max(456," << value
<< ")=" << x << std::endl; }

class B
{
public:
	B(void) { int x = max(456, value); std::cout << "max(456," << value <<
")=" << x << std::endl; }
	static const int value = 123;
};

int main(int, char *[]) { A a; B b; return 0; }
I was surprised to find the code below won't link, though both
compilers I have (g++ and aCC) give the same result so it's presumably
not a bug. Is this the problem, I've read elsewhere, about template
args having to have external linkage? But then why does the commented
out inline version work? Any help appreciated...

template <typename T> const T &max(const T &a, const T &b)
{
return (a > b ? a : b);
}

class C
{
public:
C(void); // THIS WORKS { int x = max(456, value); }

static const int value = 123;
};

C::C(void) { int x = max(456, value); } // THIS FAILS

int main(int, char *[]) { }


The linkage error is that it fails to resolve C::value.
 
N

Nick Battle

Hmmm... as I mentioned, g++ (3.3.4) and aCC (B3910B A.03.13), are both
the same and fail as I originally described.

OK. I just wondered whether I'd done something "obviously wrong".
Sounds like it's compiler dependent (so... maybe non-obviously wrong
:)

Thanks for trying it.
 
G

Greg Comeau

Hmmm... as I mentioned, g++ (3.3.4) and aCC (B3910B A.03.13), are both
the same and fail as I originally described.

OK. I just wondered whether I'd done something "obviously wrong".
Sounds like it's compiler dependent (so... maybe non-obviously wrong
:)

Thanks for trying it.

If you declare the ctor inline it might work with more compilers.
As is, it can delay the instantiation and by then doesn't know
about the constant as such. Independently, may also want to
inline max.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top