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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:03:58+00:00 2026-05-28T15:03:58+00:00

I’ve been working on some solutions for some questions that have been recently posted

  • 0

I’ve been working on some solutions for some questions that have been recently posted where the original questioners have been trying to query if a method exists in a given class. I’ve been trying to develop a solution using a SFINAE approach, but unfortunately I keep coming up empty.

Here was my attempt at a solution that isn’t working for a class that allows us to detect if another class has a method called function():

#include <iostream>

using namespace std;

struct sample_class
{
    void function() {}
};


template<typename T>
class test_size_call
{
    private:
        typedef char yes;
        typedef char (&no)[2];
        int tester[1];

        template <unsigned int>
        struct helper { static const unsigned int value = 1; };

        template<typename R>
        static yes test(int (&a)[helper<sizeof(std::declval<R>().function(), 0)>::value]);

        /* template<typename R>
        static no test(...); */

    public:
        static const bool value = (sizeof(test<T>(tester)) == sizeof(yes));
};


int main()
{
    cout << "Has function() method: " << test_size_call<sample_class>::value << endl;
        return 0;
}

The results, if you uncomment the catch-all test function, keep coming up as false. With the function commented out, I get a compiler error that there is not version of test that will take a int (&)[1] argument. I’m curious why the declval<R>().function() doesn’t seem to be instantiating properly. For instance, if I changed it to something very explicit, like declval<T>().function() then it works. Unfortunately that’s not SFINAE, because if the class doesn’t have a function() method, rather than failing silently, I get a compiler error.

I’m sure there is something really simple I’m missing here. Thanks for any help you can provide.

  • 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-28T15:03:59+00:00Added an answer on May 28, 2026 at 3:03 pm

    Must be a problem with your compiler, Clang correctly prints 1 and 0 for the following code:

    #include <utility>
    
    template<typename T>
    class test_size_call
    {
        private:
            typedef char yes;
            typedef char (&no)[2];
            int tester[1];
    
            template <unsigned int>
            struct helper { static const unsigned int value = 1; };
    
            template<typename R>
            static yes test(int (&a)[helper<sizeof(std::declval<R>().function(), 0)>::value]);
    
            template<typename R>
            static no test(...);
    
        public:
            static const bool value = (sizeof(test<T>(tester)) == sizeof(yes));
    };
    
    #include <iostream>
    
    struct sample_class
    {
        void function() {}
    };
    
    struct sample_class2{};
    
    int main()
    {
        std::cout << "Has function() method: " << test_size_call<sample_class>::value << '\n';
        std::cout << "Has function() method 2: " << test_size_call<sample_class2>::value << '\n';
        return 0;
    }
    

    While GCC 4.5.1 does not. Note that it’s fixed in GCC 4.7, as pointed out here.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.