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 9024547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:02:38+00:00 2026-06-16T06:02:38+00:00

Minimal program: #include <stdio.h> #include <type_traits> template<typename S, typename T> int foo(typename T::type s)

  • 0

Minimal program:

#include <stdio.h>

#include <type_traits>

template<typename S, typename T>
int foo(typename T::type s) {
    return 1;
}

template<typename S, typename T>
int foo(S s) {
    return 2;
}

int main(int argc, char* argv[]) {
    int x = 3;
    printf("%d\n", foo<int, std::enable_if<true, int>>(x));

    return 0;
}

output:

    1 

Why doesn’t this give a compile error? When the template code is generated, wouldn’t the functions int foo(typename T::type search) and int foo(S& search) have the same signature?

If you change the template function signatures a little bit, it still works (as I would expect given the example above):

template<typename S, typename T>
void foo(typename T::type s) {
    printf("a\n");
}

template<typename S, typename T>
void foo(S s) {
    printf("b\n");
}

Yet this doesn’t and yet the only difference is that one has an int signature and the other is defined by the first template parameter.

template<typename S, typename T>
void foo(typename T::type s) {
    printf("a\n");
}

template<typename S, typename T>
void foo(int s) {
    printf("b\n");
}

Compiler error (Clang):

test.cpp:26:2: error: call to 'foo' is ambiguous
foo<std::enable_if<true, int>>(3);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:16:6: note: candidate function [with T = std::__1::enable_if<true, int>]
void foo(typename T::type s) {
        ^
test.cpp:21:6: note: candidate function [with T = std::__1::enable_if<true, int>]
void foo(int s) {
        ^
1 error generated.

I’m using code similar to this for a project I’m working on and I’m afraid that there’s a subtly to the language that I’m not understanding that will cause some undefined behavior in certain cases. I should also mention that it does compile on both Clang and in VS11 so I don’t think it’s just a compiler bug.


Edit: Corrected second case (typo); added error message from Clang.

Edit #2: For those of you that asked what T::type means.

From http://en.cppreference.com/w/cpp/types/enable_if:

template< bool B, class T = void >
struct enable_if;

If B is true, std::enable_if has a public member typedef type, equal
to T; otherwise, there is no member typedef.

enable_if is a struct. Basically, if the expression evaluated in the first template parameter of enable_if is true (and in the case of my examples above, it is), then then there will be a public member type that has the same type as the second template parameter.

In the case of enable_if<true, int>, enable_if::type has a type of int.

  • 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-06-16T06:02:40+00:00Added an answer on June 16, 2026 at 6:02 am

    The first function is considered to be more specialized than the first.

    The function

    int foo(typename T::type)
    

    could match

    template <typename S,typename T> int foo(S s)
    

    by using T::type as the value for parameter S, but

    int foo(S s)
    

    will not match

    template <typename S,typename T> int foo(typename T::type)
    

    because T cannot be deduced.

    The logic is layed out in the C++03 standard in section 14.5.5.2, and in the C++11 standard in section 14.5.6.2.

    Here is the idea: To see if one function is more specialized than another, you invent values for each template parameter for the first function, and then see if the second function could match the resulting signature. You also invent values for the template parameters of the second function, and see if the first function will match the resulting signature. If the second function can match the first one, then the second function can’t be more specialized than the first one. If, in addition to that, the first function can’t match the second, then the first must be more specialized than the second. That is the case you have.

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

Sidebar

Related Questions

This program: #include <iostream> #include <cstdlib> #include <string> int main(int argc, const char *argv[])
I've just typed in an example from K&R... #include <stdio.h> int main (int argc,
float a, b; float sa() { return a;}; int main() { a = 10;
Possible Duplicate: GCC left shift overflow Consider the following minimal program. #include <stdint.h> #include
Here is a minimal working example describing my current situation. The file main.cpp #include
When I package a minimal Python 2.7.1/wxPython program (Hello World) with py2exe with the
Minimal code: // --------inline.h-------- struct X { static inline void foo (); }; #ifdef
A minimal code that reproduces the problem is as follows: <div class=cell> <input type=text
I have a minimal program that does little more than let me set two
Many programs return their version number with a command like: $ program --version program

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.