473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Template specialisation of static members

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
2 1878
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
1
by: Old Wolf | last post by:
I tried this code: #include <iostream> #include <string> template<typename T> struct enum_properties { static const long max; static const std::string name;
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
3
by: salem.ganzhorn | last post by:
The following code compiles cleanly, but it looks like gcc 3.4.0 does not emit the static data member into the object file (so I get a link error): #include <iostream> template <class Type>...
10
by: Frederick Gotham | last post by:
I'm trying to get a feel for template metaprogramming, and so I'm trying to write a compile-time "Raise number to positive integer" algorithm. So far, I have the following: template<class T, T...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.