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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:26:50+00:00 2026-06-17T20:26:50+00:00

Is it possible to write c++ template/macros to check whether two functions have the

  • 0

Is it possible to write c++ template/macros to check whether two functions have the same signatures (return type and arguments list) ?

Here’s a simple example of how I want to use it:

int foo(const std::string& s) {...}
int bar(const std::string& s) {...}

if (SAME_SIGNATURES(foo, bar))
{
    // do something useful... make Qt signal-slot connection for example...
}
else
{
    // signatures mismatch.. report a problem or something...
}

So is it possible somehow or is it just a pipe dream ?

P.S.
Actually I’m interesting in c++ 2003 standard.

  • 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-17T20:26:51+00:00Added an answer on June 17, 2026 at 8:26 pm

    C++11 Solution

    No need to write any template yourself.

    You can use decltype along with std::is_same:

    if (std::is_same<decltype(foo),decltype(bar)>::value )
    {
        std::cout << "foo and bar has same signature" << std::endl;
    }
    

    Here decltype returns the type of the expression which is function in this case, and std::is_same compares the two types, and returns true if both are same, else false.


    C++03 Solution

    In C++03, you don’t have decltype, so you can implement overloaded function templates as:

    template<typename T>
    bool is_same(T,T) { return true; }
    
    template<typename T, typename U>
    bool is_same(T,U) { return false; }
    

    Now you can use it as:

    if (is_same(foo, bar))
    {
        std::cout << "foo and bar has same signature" << std::endl;
    }
    

    Now that in this case is_same is a function template, not class template. So it is evaluated at runtime as opposed to compile-time. So this will give error:

    int a[is_same(foo,bar) ? 10 : 20]; //error (in Standard C++03)
                                       //the size must be known at compile-time!
    

    However, if you need to know it at compile-time, then you’ve to work more, and implement the functionality as:

    typedef char same[1];
    typedef char different[2];
    
    template<typename T>
    same& is_same_helper(T,T);  //no need to define it now!
    
    template<typename T, typename U>
    different& is_same_helper(T,U); //no definition needed!
    
    #define is_same(x,y)   (sizeof(is_same_helper(x,y)) == sizeof(same))
    

    Now use it as:

    if (is_same(foo, bar))
    {
        std::cout << "foo and bar has same signature" << std::endl;
    }
    

    You can use it at compile-time also. so you can write it:

    int a[is_same(foo,bar) ? 10 : 20]; //okay
    

    Hope that helps.

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

Sidebar

Related Questions

Is it possible to write template definition for this data type? myclass<int, myclass<int> >
Is it possible to write a C++(0x) metafunction that determines whether a type is
Possible Duplicate: Template deduction for function based on its return type? I am trying
Possible Duplicate: Is it possible to write a C++ template to check for a
Possible Duplicate: Is it possible to write a C++ template to check for a
Possible Duplicate: Is it possible to write a C++ template to check for a
I know that it's possible to have a template filter return a SafeData instance
Is it possible to write generic class with one constructor which explicitly defines type
Is it possible to use reflection when we write a code-template? I was just
Is it possible to write a function as: void func(uint64_t val) {template <typename T>

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.