IsClass?

Started by
8 comments, last by Teknofreek 18 years, 3 months ago
I'm working through "C++ Templates: The Complete Guide" and it has the following class:

template <typename T>
class IsClassT
{
    private:
        typedef char One;
        typedef struct { char a[2] } Two;
        template <typename C> static One test(int C::*);
        template <typename C> static Two test(...);
    public:
        enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
        enum { No  = !Yes };
};


My compiler won't accept it. It says it doesn't support template overload resolution during instantiation. The intent of the class is to distinguish between class and non-class types. If I move the private members out to an auxilary class I can get it to compile, but it always selects the first test template resulting in fundamental types and function pointers being identified as class types. Does anyone have any suggestions for a work around?
Keys to success: Ability, ambition and opportunity.
Advertisement
template <typename T>class IsClassT{    private:        typedef char One;        typedef struct { char a[2]; } Two;        template <typename C> static One test(int C::*);        template <typename C> static Two test(...);    public:        enum { Yes = sizeof(IsClassT<T>::template test<T>(0)) == 1 };        enum { No  = !Yes };};

This compiles with GCC 3.x and the "template" is necessary according to the C++ standard. I didn't manage to get it to compile with any Microsoft compiler, though (tested with the VC++ 2003 toolkit compiler and the VC++ 8 compiler).
Sounds like you might need a little boost (i.e. check their source)!

Enigma
I checked the Loki implementation, and they have IsClass set to the logical not of all the other types and a note that the boost one is better.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by darookie
*** Source Snippet Removed ***
This compiles with GCC 3.x and the "template" is necessary according to the C++ standard. I didn't manage to get it to compile with any Microsoft compiler, though (tested with the VC++ 2003 toolkit compiler and the VC++ 8 compiler).


Yay, one thing microsoft's compiler lost to GCC.
Apparently Boost uses basically the same approach if it works which apparently it doesn't. If it doesn't work then it uses "none of the above" to identify a class. I don't care if it is a class specifically, but rather that it isn't a fundamental type or function pointer.

I'm thinking simply to proceed, i.e. demonstrate the concept, that I'll just basically use traits for the specific types for the example. I'll worry how to do this properly when I'm doing more than following along with an example in a book.
Keys to success: Ability, ambition and opportunity.
Quote:Original post by LilBudyWizer
Apparently Boost uses basically the same approach if it works which apparently it doesn't.


That "approach" is called "abusing" the Substitution Failure Is Not An Error (SFINAE) principle, if understood well you'll understand exactly how & why such code works and where you can use it in a many various other places.

[Edited by - snk_kid on January 3, 2006 11:21:06 AM]
Let me clearify. Boost will use this technique on a compiler that will produce the correct results using this technique, but if on a compiler for which it is known that this technique does not work it will check that the type is not any of the types that are not classes to determine that it is a class. My compiler is one of the compilers for which this technique does not work.
Keys to success: Ability, ambition and opportunity.
Finally after eight hours I think I'm finally rid of this virus or it's gotten better at hiding. Attacking a guys Rhapsody is low. How am I suppose to program without music.

Ok, so anyway, I played around a bit. With the following:

template <typename T>void test(typename T::X x){}template <typename T>void test(...){}


If I try test<int>(0) I get an error saying int is not a class or structure. So I would take that to mean that with my compiler that substitution failure is an error.
Keys to success: Ability, ambition and opportunity.
I did a quick search and found this:

http://www.codecomments.com/archive272-2005-2-386191.html

It looks like the yes enum needs to look like this:

enum { Yes = sizeof(IsClassT<T>::template test<T>(0)) == 1 };


I've never seen a statement like that, where the template keyword is used when calling a templated function. That's just bizarre. I guess I need to read up more on templates myself ;)

-John
- John

This topic is closed to new replies.

Advertisement