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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:41:41+00:00 2026-06-10T19:41:41+00:00

I am basically trying to do the same as std::enable_if : parameter vs template

  • 0

I am basically trying to do the same as std::enable_if : parameter vs template parameter but I can’t get my code to compile.

I had a simple first version that has the std::enable_if in the parameters and which works fine:

#include <iostream>
#include <type_traits>

template <typename T>
void foo(T t, typename std::enable_if< std::is_same<T, int>::value >::type* = 0) {
  std::cout << "int" << std::endl;
}

template <typename T>
void foo(T t, typename std::enable_if< !std::is_same<T, int>::value >::type* = 0) {
  std::cout << "not int" << std::endl;
}

int main(int argc, char* argv[])
{
  foo(10);
  foo(10.1);
  return 0;
}

But I thought it might be more concise if the template stuff was in one place and wanted the enable_if out of the function arguments.

Now if I simply move the enable_if part I get the following:

#pragma warning(1 : 4519)

#include <iostream>
#include <type_traits>

template <typename T, typename std::enable_if< std::is_same<T, int>::value >::type = 0>
void foo(T t) {
  std::cout << "int" << std::endl;
}

template <typename T, typename std::enable_if< !std::is_same<T, int>::value >::type = 0>
void foo(T t) {
  std::cout << "not int" << std::endl;
}

int main(int argc, char* argv[])
{
  foo(10);
  foo(10.1);
  return 0;
}

The #pragma warning(1 : 4519) I needed because otherwise default arguments on a function template are an error in VS2010. The problem is it still doesn’t compile.
The first message was: error C2783: 'void foo(T)' : could not deduce template argument for '__formal'
and even though I don’t want to do that I tried explicitly stating which template I want by calling it using

  foo<int, int>(10);
  foo<double, double>(10.1);

but it still doesn’t work and the new error is.

error C2975: 'foo' : invalid template argument for 'unnamed-parameter', expected compile-time constant expression

I hope somebody can tell me how to fix this and of course all comments on my style and other issues my code might have are welcome. 🙂

Extra question: Does anybody know why VS2010 does not allow default arguments on function templates by default?

  • 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-10T19:41:43+00:00Added an answer on June 10, 2026 at 7:41 pm

    The problem is that the second template parameter in std::enable_if defaults to void. So your doing something which is pretty much the same as:

    template <typename T, void = 0>
    

    Which makes substitution fail always. You could use a non-type template argument of type int, which you can give a default 0 value:

    template <typename T, typename std::enable_if< std::is_same<T, int>::value, int >::type = 0>
    

    Do the same for both overloads, and it will work.

    Demo here.

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

Sidebar

Related Questions

Basically I am trying the same as this guy: How can I tell proguard
I am basically trying to implement a Strategy pattern, but I want to pass
I am trying to basically the same as poupou has shown on this previous
Basically I'm trying to get a rankings between 4 people. I have an array
I am basically trying to combine code from two examples given in the DataTables
today I am basically trying to create some JAVA code that moves an actor
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
I'm trying to basically trying to separate a specific amount of text from the
I'm basically trying to iterate through a dict and print out the key /
I'm basically trying to extract a Mac application from a zip file using QuaZip.

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.