Outside access to ACTUAL template parameter

  • Thread starter Frederick Gotham
  • Start date
F

Frederick Gotham

The following won't compile for me with any of my compilers:

template<class SpecialType>
class MyClass {};


int main()
{
MyClass<int>::SpecialType obj;
}


The following compiles with Microsoft Visual C++, but I get a build error
with g++. According to the Standard, should it compile?


template<class SpecialType>
class MyClass {
public:

typedef SpecialType SpecialType;

};


int main()
{
MyClass<int>::SpecialType obj;
}


If the Standard forbids this, then the less favourable work-around would
be:


template<class SpecialType_T>
class MyClass {
public:

typedef SpecialType_T SpecialType;
};


int main()
{
MyClass<int>::SpecialType obj;
}


But I don't want to do that until I know whether the Standard forbids my
second code snippet above.

(I'm writing reusable code, and at some point in the future, I may change
the internals so that "SpecialType" no longer coincides with the template
parameter, which is why I have the client write:

MyClass<int>::SpecialType obj;

rather than simply:

int obj;
 
C

Cy Edmunds

Frederick Gotham said:
The following won't compile for me with any of my compilers:

template<class SpecialType>
class MyClass {};


int main()
{
MyClass<int>::SpecialType obj;
}


The following compiles with Microsoft Visual C++, but I get a build error
with g++. According to the Standard, should it compile?


template<class SpecialType>
class MyClass {
public:

typedef SpecialType SpecialType;

};

I don't know what if anything the standard says about this but it seems to
me like a bad idea anyway. The meaning of this typedef looks like a Zen
puzzle to me. I think you can see why compilers might have a problem with
it, and it's completely unnecessary.
int main()
{
MyClass<int>::SpecialType obj;
}


If the Standard forbids this, then the less favourable work-around would
be:


template<class SpecialType_T>
class MyClass {
public:

typedef SpecialType_T SpecialType;
};


int main()
{
MyClass<int>::SpecialType obj;
}


But I don't want to do that until I know whether the Standard forbids my
second code snippet above.

I usually typedef my template parameters in the class pretty much as you
show above. It never hurts and sometimes saves my bacon when I'm writing
other template classes and functions.

Look in the standard library implementations of classes. They do this a LOT.
 
R

Robbie Hatley

Frederick Gotham said:
The following won't compile for me with any of my compilers:

template<class SpecialType>
class MyClass {};


int main()
{
MyClass<int>::SpecialType obj;
}

The following compiles with Microsoft Visual C++, but I get a
build error with g++.
According to the Standard, should it compile?

I don't see anything in section 14 (Templates) that requires
the code you give to compile, no.
template<class SpecialType>
class MyClass {
public:

typedef SpecialType SpecialType;

};


int main()
{
MyClass<int>::SpecialType obj;
}


If the Standard forbids this,

Say, rather, the std. doesn't seem to require compilers to recognize
your syntax.
then the less favourable work-around would be:

template<class SpecialType_T>
class MyClass {
public:

typedef SpecialType_T SpecialType;
};


int main()
{
MyClass<int>::SpecialType obj;
}


But I don't want to do that...

Why not? If you look at any STL reference book, you'll see that
many template classes in STL have "value_type" defined. That seems
to be the way to do it. So why don't you just do this:

#include <iostream>
template<typename T>
class MyClass
{
public:
typedef T value_type;
MyClass(T a) : a_(a) {}
T get_a() {return a_;}
private:
T a_;
};

int main()
{
MyClass<int>::value_type Object = 7;
MyClass<int> Widget (Object);
std::cout << Widget.get_a() << std::endl;
return 0;
}

(A fancy way to display "7".)

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top