Re: conflicting declaration in template static member
"Michael Doubez" <michael.doubez@free.fr> wrote in message
news:52efd416-36f1-4a58-9781-d7892caacae7@z20g2000yqe.googlegroups.com...
On 25 mar, 01:04, "Paul" <pchris...@yahoo.co.uk> wrote:
"Michael Doubez" <michael.dou...@free.fr> wrote in message
On 24 mar, 19:07, "Paul" <pchris...@yahoo.co.uk> wrote:
AFAICT you do not need:
template< class T > int const Foo<T>::N; /* This is instanciated
implicitly
with Foo<Bar> f anyway*/
--No it is not implicity instanciated without a definition.
--It is true that I don't really need it, provided I don't take a
--reference to it: the compiler directly reuse the value specified at
--declaration time; but it is formally UB.
How would taking a reference make any difference?
Does the template make any differenece to if you just did:
struct Foo{
static int const N = 42;
};
Try it out:
#include <iostream>
struct Foo{
// link fails with this line
static int const N = 42;
// compiles with this line
// enum { N = 42 };
};
void print( int const & v ) { std::cout<<v<<'\n';}
int main() {
print(Foo::N);
}
I did try it out after I posted, and it worked just fine, what compiler did
you say to avoid again? :-)
Here's the code I posted , maybe that compile of yours isn't too good.
struct Bar1 { enum { size = 42 } ;};
struct Bar2 { enum { size = 52 } ;};
template< class T >
struct Foo{
static int const N= T::size;
static int values[N];
public:
Foo(){}
template<typename U>
void func(Foo<U>& para);
};
//template< class T > int const Foo<T>::N;
template<class T> int Foo<T>::values[Foo<T>::N]={0};
template<typename T>
template<typename U>
void Foo<T>::func(Foo<U>& para){
std::cout<< "\nthis->N: \t" << this->N;
std::cout<< "\nthat.N: \t" << para.N;
}
void funct(int const & r){
std::cout<<"\nIn funct, value of parameter = " << r;
}
int main() {
Foo<Bar1> f1;
Foo<Bar2> f2;
funct(f1.N);
funct(f2.N);
f1.func(f2);
f2.func(f1);
}
I also tried another couple of ways to use it as a reference and all seemed
to compile ok with Microsoft v14 command line compiler.
And I don't think that version is too good because i have had problems with
it compiling templates in the form of :
template<template<class T> class U, classT>
When I go onto my newer VS2010 these compile just fine , so that old command
line compiler I've been using to test your code isn't the greatest but it
seems to compile/link your code fine without
template< class T > int const Foo<T>::N;
HTH