Question

I came across some code written in VS7.1 and now I'm trying to get it to work for MacOSX. The code snippet below I understand is about the SFINAE principle. From what I understand, the code is used to at compile-time know what type something is by relying on some template instantiation magic. In short, the right overload is picked by looking at the template argument.

Here's the code I have. Somewhat simplified to only show the problem.

template <typename T>
struct SomeClass
{
};

template <>
struct SomeClass<char>
{
    typedef char Type;
};

template <typename T>
struct IsChar
{
    typedef char Yes;
    typedef int No;

    template <typename U>
    static Yes Select(U*, typename SomeClass<U>::Type* p = 0);
    template <typename U>
    static No Select(U*, ...);
    static T* MakeT();

    const static bool Value = sizeof(Select(MakeT())) == sizeof(Yes);
};

I'm simply using this like this:

if (IsChar<int>::Value)
{
    ...

When compiling the above code works well and it picks the topmost class due to the missing typedef for Type when using int.

If I now use char instead...

if (IsChar<char>::Value)
{
    ...

...the compiler will complain about ambiguous Select functions, because it doesn't know which one to use. From what I've read overload resolution gives least preference to the ellipsis parameter (...). Thus, it should know to select the first one.

The code was working fine on at least VS7.1, but not on gcc for MacOSX and not gcc4.4 for Linux.

Any suggestions how to correct this? Maybe it's usually done in another way?

Thanks!

UPDATE: I realized that the sample code I gave is maybe slightly too much simplified, because I believe we're not jsut checking for type here even if I mistakenly make it look like that. I'll have to gather a bit more information for you tonight as I don't have the code here. Sorry for that.

UPDATE2: Even if my presenation in bad and it's due to not being familiar with the original code or using templates this way. Meanwhile I dig out a bit more information, let's assume these constructs are there for some reason X and the names I have given are all wrong, what about the compiler problem? Why is it not able to select the right overloaded function here? This is also interesting me. As I said, I'll get back with a better explanation what the overall aim is.

Edit

After taking closer look at the original code, it is using boost::integral_constant and also boost::enable_if like what was suggested here. The problem is something specific to how the template arguments are deduced and it didn't work the way it was set up. However, following what Georg suggested in the end of his answer I could correct things to accept things. I have now the following:

typedef char Yes;
typedef int No;

template <typename U> static Yes Select(typename SomeClass<U>::Type* p);
template <typename U> static No Select(...);

static const bool Value = sizeof(Select<T>(0)) == sizeof(Yes);

This works well. While experimenting a bit I found out that having two function parameters in the Select functions results in a problem. I haven't found the reason. I'll come back to this when I understand things better.

Thanks for all your help. At least I understand the principles here now and how things should work. Only some details, which are still unknown.

Was it helpful?

Solution

Unless i'm misunderstanding the intent, the above usage sample doesn't require use of SFINAE. If you only want to statically assert the type of Type you can just use something like this:

template<class T1, class T2> struct SameType {
    static const bool Value = false;
};

template<class T> struct SameType<T, T> {
    static const bool Value = true;
};

template <typename T>
struct IsChar {
    static const bool Value = SameType<T, char>::Value;
};

If you really need to use this for SFINAE (i.e. disabling/enabling functions based on template parameters), just use the above in combination with something like Boosts enable_if:

template<class T> 
typename boost::enable_if_c<IsChar<T>::Value, void>::type
someFunction() {
}

Or if you can go Boost all the way:

template<class T> 
typename boost::enable_if<boost::mpl::is_same<T, char>, void>::type
someFunction() {
}

Update:

Rereading this, if you actually wanted to check wether a specialization of SomeClass has a typedef Type, you should be able to use the solution from over here:

template<class T> struct HasType {
    template<class U> static char (&test(typename U::Type const*))[1];
    template<class U> static char (&test(...))[2];
    static const bool Value = (sizeof(test< SomeClass<T> >(0)) == 1);
};

In that case IsChar is certainly a misnomer though, something like HasType or HasTypedefType would be more descriptive :)

OTHER TIPS

In my comment I said that you don't typically use the results of these predicates as values for an if, and this is why:

// assume we have has_typedef_type from the Wikipedia page:

template <typename T>
void foo(const T& x)
{
    if (has_typedef_type<T>::value)
    {
        // the predicate is true, so T::type exists
        typename T::type y = x;
    }
    else
    {
        // the predicate is false, so T::type doesn't exist, do something else
        float y = x;
    }
}

This may look fine, but consider when the predicate is false, the compiler is going to try and compile this:

// let's say we called it with double
void foo<double>(const double& x)
{
    if (false)
    {
        // wait, double doesn't have this!
        typename double::type y = x;
    }
    else
    {
        float y = x;
    }
}

The problem is that the code, even though it'll be removed with dead-code removal, is ill-formed. The solution is to make the if compile-time as well, but first some boiler-plate:

// in C++0x, these are defined in <type_traits>, but we'll do it ourselves
// (Boost has these as well)
typename <typename T, T Value>
struct integral_constant
{
    typedef T type;
    static const T value = Value;
};

typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;

With that out of the way, we define our functions:

namespace detail
{
    // here are the real implementations
    template <typename T>
    void foo(const T& x, true_type)
    {
        // the predicate is true, so T::type exists
        typename T::type y = x;
    }

    template <typename T>
    void foo(const T& x, false_type)
    {
        // the predicate is false, so T::type doesn't exist, do something else
        float y = x;
    }
}

template <typename T>
void foo(const T& x)
{
    detail::foo(x, // chose which function to call, using the type of this:
                integral_constant<bool, has_typedef_type<T>::value>());
}

Now everything is okay, because the branches are completely independent from one another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top