Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3843716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:58:39+00:00 2026-05-19T15:58:39+00:00

I came across some code written in VS7.1 and now I’m trying to get

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:58:40+00:00Added an answer on May 19, 2026 at 3:58 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across this in some code - basically someone trying to create
I was doing some code review today and came across an old code written
I came across some code written in C that looks like this: if (file
I came across some PHP code that was written by a co-worker (it was
While debugging javascript written by someone else, I came across some code that I've
I was looking through some code for work, and came across this line: eval(\$element
Whilst refactoring some code I came across some getter methods that returns a std::string.
A while ago, I came across some code that marked a data member of
An interesting issue came up recently. We came across some code that is using
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.