Click to See Complete Forum and Search --> : explicit template qualifier
kakalake
April 28th, 2003, 01:52 PM
Hello!
How can i use the following function in the following class?
class test
{
public:
template<class T>
T testFunc();
};
I tried to invoke the function like this:
test t;
1) t.testFunc();
2) t.testFunc<char>();
3) t.template testFunc<char>();
Everytime i tried to invoke the function testFunc in one of the 3 ways i get an error. Why ?
I am asking this in refer to the class bitset. I tried to use the to_string function and tried to call it in this way:
bitSet.to_string<std::string::value_type, ....>();
But it doesn´t work.
Could someone help me?
I am using Vc++6.0 and the stlport4.5.3 library.
Thanks in advance...
PaulWendt
April 28th, 2003, 09:22 PM
Are you using Service Pack 5 for Visual Studio 6? Try putting the
function's definition inline. I don't mean putting it later in the
header file, either; I mean put it right where you first declare the
function. Visual Studio 6.0 has weird bugs like that sometimes;
it's not the best when it comes to templates.
Method #2 seems to be the correct way to call your function.
--Paul
Gabriel Fleseriu
April 28th, 2003, 11:22 PM
The method 3) is the correct syntax (see TC++PL, 3rd ed.). However, VC++6.0 doesn't support (has a very limited support of) member templates. Sorry.
PaulWendt
April 29th, 2003, 05:36 AM
Originally posted by Gabriel Fleseriu
The method 3) is the correct syntax (see TC++PL, 3rd ed.). However, VC++6.0 doesn't support (has a very limited support of) member templates. Sorry.
When you're calling the function, you type:
t.template testFunc<char>();
Are you sure? If so, what page is this on?
--Paul
Gabriel Fleseriu
April 29th, 2003, 05:46 AM
Originally posted by PaulWendt
When you're calling the function, you type:
t.template testFunc<char>();
Are you sure? If so, what page is this on?
--Paul
Yes, I'm to 99.9% sure. I don't have the book handy, but I'll come back to this. If you want to look for yourself: Bjarne presents the implementation of an allocator using a MemoryPool class. It's there, somewhere. I remember him commenting on this being a somewhat strange syntax.
PaulWendt
April 29th, 2003, 08:19 AM
Originally posted by Gabriel Fleseriu
Yes, I'm to 99.9% sure. I don't have the book handy, but I'll come back to this. If you want to look for yourself: Bjarne presents the implementation of an allocator using a MemoryPool class. It's there, somewhere. I remember him commenting on this being a somewhat strange syntax.
Wow. I'll go look it up; that doesn't even look like a function call.
Doesn't #2 work? I've been using that syntax on Visual Studio
.NET and g++ and it has been working so far.
--Paul
Gabriel Fleseriu
April 29th, 2003, 09:27 AM
Page 858
PaulWendt
April 29th, 2003, 09:59 AM
Thanks, Gabriel.
Now I have to go correct all of this undefined behavior I've been
relying upon. Stroustrup even cites #2 as an example of a
syntax error. Whoops :)
PaulWendt
April 29th, 2003, 10:28 AM
Hm. Looking at it again, it appears that there is a slight difference
between his example and what I've been doing the entire time.
Here's Stroustrup's example [for everyone else to see, if they
don't posess the book]:
class Memory { // some Allocator
public:
template<class T> T* get_new();
template<class T> void release(T&);
// ...
};
template<class Allocator> void f(Allocator& m)
{
int* p1 = m.get_new<int>(); // syntax error: int after less-than operator
int* p2 = m.template get_new<int>(); // explicit qualitification
// ...
m.release(p1);
m.release(p2);
}
Now, the difference is that get_new is prototyped to return type
T*, yet its template parameter is just type T. For clarification
purposes ... please look at my example.
class Paul
{
public:
template <typename T>
T convert(const std::string& from);
};
Now, to use it:
Paul p;
int i = p.convert<int>("3435"); // is this undefined? should this be an error?
I'm just wondering if it has to look like this instead:
Paul p;
int i = p.template convert<int>("3435");
Thanks for the enlightenment, Gabriel. I never read the
appendices in his book ... and now I've got some weekend
reading, it seems :)
--Paul
kakalake
April 29th, 2003, 01:00 PM
Thanks a lot to all that tried to help me especially to Paul and Gabriel.
I tried to compile my code with the gcc 3.2 compiler and the following both versions works.
2) t.testFunc<char>();
3) t.template testFunc<char>();
Thanks alot
PaulWendt
April 29th, 2003, 01:52 PM
I'm still not sure if the second form there is absolutely
necessary [the one with the secrewy template thing]. The first
form even compiled on the Comeau compiler ... so I'm thinking
that you only need to put .template there when your return
type doesn't exactly match the type [like if you're returning T*
instead of T]. I don't know for sure, though.
--Paul
Gabriel Fleseriu
April 30th, 2003, 12:38 AM
According to the Standard the second version, that ist.testFunc<char>(); should produce a compile error because the '<' I marked red should be parsed as the 'less than' operator.
PaulWendt
April 30th, 2003, 05:10 AM
Thanks, Gabriel. I guess this isn't the first time that three
compilers have been unanimously wrong about something. I'm
just happy that I've found out for sure now rather than two years
down the road when one [or all] of these compilers have
corrected the behavior.
--Paul
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.