Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializing static const variables of template classes

Reply
Thread Tools

Initializing static const variables of template classes

 
 
david.baird
Guest
Posts: n/a
 
      03-17-2007
I have 3 classes, 1 which does not use templates, CGood, and 2 which
do use templates, CBad, and COkay. I believe that all of them should
work, but only CGood and COkay are working for me. CBad fails to
compile.

class CGood
{
public:
typedef int arr_t[2];
static const arr_t bar;
};

const CGood::arr_t CGood::bar = { 1, 2 };

template <class T>
class CBad
{
public:
typedef T arr_t[2];
static const arr_t bar;
};

template <class T>
// g++ says ``error: expected initializer before 'CBad''':
const CBad<T>::arr_t CBad<T>::bar = { 1, 2 };

template <class T>
class COkay
{
public:
typedef T arr_t[2];
static const arr_t bar;
};

template <class T>
const T COkay<T>::bar[2] = { 1, 2 };

Can anyone tell me if there is something is wrong (or right) with
CBad? It seems fine to me, but the compiler just doesn't like it.

If it is helpful, here is my g++ version:

$ g++ --version
g++ (GCC) 4.1.1 (Gentoo 4.1.1-r3)

 
Reply With Quote
 
 
 
 
david.baird
Guest
Posts: n/a
 
      03-17-2007
Well, I just figured it out Maybe someone will find this
useful...

On Mar 17, 1:17 am, "david.baird" <(E-Mail Removed)> wrote:
> template <class T>
> // g++ says ``error: expected initializer before 'CBad''':
> const CBad<T>::arr_t CBad<T>::bar = { 1, 2 };


Instead, use the typename keyword:

template <class T>
const typename CBad<T>::arr_t CBad<T>::bar = { 1, 2 };


This website says some nice things about typename (though I am still
trying to wrap my head around it):

http://www.gotw.ca/gotw/035.htm
GotW #35

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
static const members of template classes? Steven T. Hatton C++ 3 10-01-2004 08:06 AM



Advertisments