By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,083 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,083 IT Pros & Developers. It's quick & easy.

Template specialisation of static members

P: n/a
Another "template newbie" question. I would like to write the following
code :

-------------
template<typename T, typename U>
struct Test{

static const bool Cond;

};

template<typename T, typename U>
const bool Test<T,U>::Cond = false;

template<typename T>
const bool Test<T,T>::Cond = true;
------------------

As you might expect, it doesn't work, at least under Microsoft Visual
C++ 2005 Express Edition. More precisely, it gives me the following
error at the last line of the code :

error C3860: template argument list following class template name must
list parameters in the order used in template parameter list

Can someboby explain me what I have done wrong ?

Sincerely,

Helfer Thomas
Apr 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Can someboby explain me what I have done wrong ?


I think what you want is to specialise the class:

template<typename T, typename U>
struct Test{
static const bool Cond = false;
};

template<typename T>
struct Test<T,T>{
static const bool Cond = true;
} ;

I also provide a suggested alternative which is **unofficially as yet
** becoming the accepted format for doing this type of thing. You will
need the http://www.boost.org libraries to compile where you can read
more about it in docs too:

// alternate using socalled 'metafunction forwarding'
// and mechanism proposed for standardisation
// 'value' member name is common convention too

#include <boost/type_traits/integral_constant.hpp>
template<typename T, typename U>
struct Test1 : boost::integral_constant<bool,false>{};
template<typename T>
struct Test1<T,T> : boost::integral_constant<bool,true>{};
// check it all works
#include <iostream>

int main()
{
std::cout << Test<int,double>::Cond <<'\n';
std::cout << Test<int,int>::Cond <<'\n';

//***standardised*** format
std::cout << Test1<int,double>::value <<'\n';
std::cout << Test1<int,int>::value <<'\n';
}

regards
Andy little

Apr 15 '06 #2

P: n/a
helfer thomas wrote:
Another "template newbie" question. I would like to write the
following code :

-------------
template<typename T, typename U>
struct Test{

static const bool Cond;

};

template<typename T, typename U>
const bool Test<T,U>::Cond = false;

template<typename T>
const bool Test<T,T>::Cond = true;
------------------


As Andy explained, you cannot specialise a member without specialising
the whole class. However, there is an alternative even to the Boost-
based solution he suggested. Use supplemental template:

template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };

template<typename T, typename U>
struct Test {
static cosnt bool Cond = same<T,U>::yes;
... // if you need more stuff here
};

Don't forget to define the 'Cond' member outside the class if you do
take its address anywhere.

Now, this technique allows you to avoid redefining the entire 'Test'
class just to give 'Cond' static member different values for different
specialisations.

Good luck!

V
--
Please remove capital As from my address when replying by mail
Apr 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.