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

  • Home
  • SEARCH
  • 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 553629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:37:31+00:00 2026-05-13T11:37:31+00:00

I have some trouble forward declaring a function that uses boost::enable_if : the following

  • 0

I have some trouble forward declaring a function that uses boost::enable_if: the following piece of code gives me a compiler error:

// Declaration
template <typename T>
void foo(T t);

// Definition
template <typename T>
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)
{
}

int main()
{
    foo(12);
    return 0;
}

When compiling, I get an “ambiguous call to foo” error. According to the definition of enable_if, the ‘type’ typedef corresponds to void when the condition is true, so as far as I can see, the two signatures of foo match. Why does the compiler think they are different, and is there a correct way to forward declare foo (preferably without repeating the enable_if part)?

  • 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-13T11:37:32+00:00Added an answer on May 13, 2026 at 11:37 am

    This is not only a problem with enable_if. You get the same error on Visual Studio and gcc with the following code:

    struct TypeVoid {
      typedef void type;
    };
    
    template<typename T>
    void f();
    
    template<typename T>
    typename T::type f() {
    }
    
    int main()
    {
      f<TypeVoid>();
      return 0;
    }
    

    I think the main problem is that the return type (before instantiation) is part of the signature of a template function. There is more information here.

    Regarding your code, if the declaration refers to the definition, you should match both:

    // Declaration       
    template <typename T>       
    typename boost::enable_if<boost::is_same<T, int> >::type foo(T t);       
    
    // Definition       
    template <typename T>       
    typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)       
    {       
    }
    

    If the declaration refers to a different function, the compiler would never be able to choose the correct one for ints, because they both are valid. However, you can disable the first one for ints using disable_if:

    // Other function declaration
    template <typename T>
    typename boost::disable_if<boost::is_same<T, int> >::type foo(T t);
    
    // Defition
    template <typename T>       
    typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)       
    {       
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 444k
  • Answers 444k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are on the right track. However, there are a… May 15, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer You're using the auto-generated service reference. Update the service reference… May 15, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer Why in the world are you migrating to MySQL 4.0?… May 15, 2026 at 6:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.