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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With