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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:31:35+00:00 2026-05-20T06:31:35+00:00

Is it possible to write a C++(0x) metafunction that determines whether a type is

  • 0

Is it possible to write a C++(0x) metafunction that determines whether a type is callable?

By callable type I mean a function type, function pointer type, function reference type (these are detected by boost::function_types::is_callable_builtin), lambda types, and any class with an overloaded operator() (and maybe any class with an implicit conversion operator to one of these, but that’s not absolutely necessary).

EDIT: The metafunction should detect the presence of an operator() with any signature, including a templated operator(). I believe this is the difficult part.

EDIT: Here is a use case:

template <typename Predicate1, typename Predicate2>
struct and_predicate
{
    template <typename ArgT>
    bool operator()(const ArgT& arg)
    {
        return predicate1(arg) && predicate2(arg);
    }

    Predicate1 predicate1;
    Predicate2 predicate2;
};

template <typename Predicate1, typename Predicate2>
enable_if<ice_and<is_callable<Predicate1>::value,
                  is_callable<Predicate2>::value>::value,
          and_predicate<Predicate1, Predicate2>>::type
operator&&(Predicate1 predicate1, Predicate2 predicate2)
{
    return and_predicate<Predicate1, Predicate2>{predicate1, predicate2};
}

is_callable is what I would like to implement.

  • 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-20T06:31:36+00:00Added an answer on May 20, 2026 at 6:31 am

    The presence of a non-templated T::operator() for a given type T can be detected by:

    template<typename C> // detect regular operator()
    static char test(decltype(&C::operator()));
    
    template<typename C> // worst match
    static char (&test(...))[2];
    
    static const bool value = (sizeof( test<T>(0)  )
    

    The presence of a templated operator can be detected by:

    template<typename F, typename A> // detect 1-arg operator()
    static char test(int, decltype( (*(F*)0)( (*(A*)0) ) ) = 0);
    
    template<typename F, typename A, typename B> // detect 2-arg operator()
    static char test(int, decltype( (*(F*)0)( (*(A*)0), (*(B*)0) ) ) = 0);
    
    // ... detect N-arg operator()
    
    template<typename F, typename ...Args> // worst match
    static char (&test(...))[2];
    
    static const bool value = (sizeof( test<T, int>(0)  ) == 1) || 
                              (sizeof( test<T, int, int>(0)  ) == 1); // etc...
    

    However, these two do not play nicely together, as decltype(&C::operator()) will produce an error if C has a templated function call operator. The solution is to run the sequence of checks against a templated operator first, and check for a regular operator() if and only if a templated one can not be found. This is done by specializing the non-templated check to a no-op if a templated one was found.

    template<bool, typename T>
    struct has_regular_call_operator
    {
      template<typename C> // detect regular operator()
      static char test(decltype(&C::operator()));
    
      template<typename C> // worst match
      static char (&test(...))[2];
    
      static const bool value = (sizeof( test<T>(0)  ) == 1);
    };
    
    template<typename T>
    struct has_regular_call_operator<true,T>
    {
      static const bool value = true;
    };
    
    template<typename T>
    struct has_call_operator
    {
      template<typename F, typename A> // detect 1-arg operator()
      static char test(int, decltype( (*(F*)0)( (*(A*)0) ) ) = 0);
    
      template<typename F, typename A, typename B> // detect 2-arg operator()
      static char test(int, decltype( (*(F*)0)( (*(A*)0), (*(B*)0) ) ) = 0);
    
      template<typename F, typename A, typename B, typename C> // detect 3-arg operator()
      static char test(int, decltype( (*(F*)0)( (*(A*)0), (*(B*)0), (*(C*)0) ) ) = 0);
    
      template<typename F, typename ...Args> // worst match
      static char (&test(...))[2];
    
      static const bool OneArg = (sizeof( test<T, int>(0)  ) == 1);
      static const bool TwoArg = (sizeof( test<T, int, int>(0)  ) == 1);
      static const bool ThreeArg = (sizeof( test<T, int, int, int>(0)  ) == 1);
    
      static const bool HasTemplatedOperator = OneArg || TwoArg || ThreeArg;
      static const bool value = has_regular_call_operator<HasTemplatedOperator, T>::value;
    };
    

    If the arity is always one, as discussed above, then the check should be simpler. I do not see the need for any additional type traits or library facilities for this to work.

    • 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 a C function that does the following? Allocate a
Possible Duplicate: Write a function that returns the longest palindrome in a given string
Is it possible to write a doctest unit test that will check that an
Is it possible to write code in a Flex application that will only be
Is it possible to write a regular expression that matches a nested pattern that
Would it be possible to write a class that is virtually indistinguishable from an
It's possible to write Markdown content with invalid syntax. Invalid means that the BlueCloth
Is it possible to write a template that changes behavior depending on if a
I was wondering if i could possible write an app, that could be a
Is it possible to write a lambda expression that will iterate through array of

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.