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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:11:28+00:00 2026-06-14T09:11:28+00:00

I am trying to determine which version of a member function gets called based

  • 0

I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this:

#include <iostream>
#include <type_traits>

template<typename T>
struct Point
{
  void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0)
  {
    std::cout << "T is int." << std::endl;
  }

  void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0)
  {
    std::cout << "T is not int." << std::endl;
  }
};

int main()
{
  Point<int> intPoint;
  intPoint.MyFunction();

  Point<float> floatPoint;
  floatPoint.MyFunction();
}

which I thought is saying “use the first MyFunction if T is int, and use the second MyFunction if T is not int, but I get compiler errors saying “error: no type named ‘type’ in ‘struct std::enable_if’”. Can anyone point out what I am doing wrong here?

  • 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-14T09:11:29+00:00Added an answer on June 14, 2026 at 9:11 am

    enable_if works because the substitution of a template argument resulted in an error, and so that substitution is dropped from the overload resolution set and only other viable overloads are considered by the compiler.

    In your example, there is no substitution occurring when instantiating the member functions because the template argument T is already known at that time. The simplest way to achieve what you’re attempting is to create a dummy template argument that is defaulted to T and use that to perform SFINAE.

    template<typename T>
    struct Point
    {
      template<typename U = T>
      typename std::enable_if<std::is_same<U, int>::value>::type
        MyFunction()
      {
        std::cout << "T is int." << std::endl;
      }
    
      template<typename U = T>
      typename std::enable_if<std::is_same<U, float>::value>::type
        MyFunction()
      {
        std::cout << "T is not int." << std::endl;
      }
    };
    

    Edit:

    As HostileFork mentions in the comments, the original example leaves the possibility of the user explicitly specifying template arguments for the member functions and getting an incorrect result. The following should prevent explicit specializations of the member functions from compiling.

    template<typename T>
    struct Point
    {
      template<typename... Dummy, typename U = T>
      typename std::enable_if<std::is_same<U, int>::value>::type
        MyFunction()
      {
        static_assert(sizeof...(Dummy)==0, "Do not specify template arguments!");
        std::cout << "T is int." << std::endl;
      }
    
      template<typename... Dummy, typename U = T>
      typename std::enable_if<std::is_same<U, float>::value>::type
        MyFunction()
      {
        static_assert(sizeof...(Dummy)==0, "Do not specify template arguments!");
        std::cout << "T is not int." << std::endl;
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to determine which combination of packages to use for a
I am trying to determine which element with a particular class is visible. Then
I am trying to determine which implementation of the data structure would be best
I am trying to determine the type of a property which an attribute relates
I am trying to determine if a phone is located in this polygon using
I'm trying to determine what version of the TWAIN specification I can write my
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have several hosting accounts with different companies, and I'm trying to evaluate which
I'm trying to determine the version number of my Silverlight application. Currently I am
I am trying to determine the version of the .NET framework that a dll

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.