473,387 Members | 1,516 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,387 software developers and data experts.

Outside access to ACTUAL template parameter


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;

--

Frederick Gotham
Jul 1 '06 #1
2 1143

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:RZ*******************@news.indigo.ie...
>
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.
>
(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;

--

Frederick Gotham

Jul 2 '06 #2
"Frederick Gotham" <fg*******@SPAM.comwrote:
The following won't compile for me with any of my compilers:

template<class SpecialType>
class MyClass {};
int main()
{
MyClass<int>::SpecialType obj;
}
The class "MyClass<int>" has no member neamed "SpecialType",
so I wouldn't expect this to compile.
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<intWidget (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/
Jul 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lionel B | last post by:
Greetings. In a template class for which a template parameter may be another template class, I would like to be have access to the type of the nested template parameter. Hopefully the following...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.