Jump to content
  • Advertisement
Sign in to follow this  
LilBudyWizer

IsClass?

This topic is 4672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!