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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:43:25+00:00 2026-05-30T16:43:25+00:00

In https://stackoverflow.com/a/1967183/134841 , a solution is provided for statically checking whether a member exists,

  • 0

In https://stackoverflow.com/a/1967183/134841, a solution is provided for statically checking whether a member exists, possibly in a subclass of a type:

template <typename Type> 
class has_resize_method
{ 
   class yes { char m;}; 
   class no { yes m[2];}; 
   struct BaseMixin 
   { 
     void resize(int){} 
   }; 
   struct Base : public Type, public BaseMixin {}; 
   template <typename T, T t>  class Helper{}; 
   template <typename U> 
   static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0); 
   static yes deduce(...); 
public: 
   static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0))); 
};

However, it doesn’t work on C++11 final classes, because it inherits from the class under test, which final prevents.

OTOH, this one:

template <typename C>
struct has_reserve_method {
private:
    struct No {};
    struct Yes { No no[2]; };
    template <typename T, typename I, void(T::*)(I) > struct sfinae {};
    template <typename T> static No  check( ... );
    template <typename T> static Yes check( sfinae<T,int,   &T::reserve> * );
    template <typename T> static Yes check( sfinae<T,size_t,&T::reserve> * );
public:
    static const bool value = sizeof( check<C>(0) ) == sizeof( Yes ) ;
};

fails to find the reserve(int/size_t) method in baseclasses.

Is there an implementation of this metafunction that both finds reserved() in baseclasses of T and still works if T is final?

  • 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-30T16:43:26+00:00Added an answer on May 30, 2026 at 4:43 pm

    Actually, things got much easier in C++11 thanks to the decltype and late return bindings machinery.

    Now, it’s just simpler to use methods to test this:

    // Culled by SFINAE if reserve does not exist or is not accessible
    template <typename T>
    constexpr auto has_reserve_method(T& t) -> decltype(t.reserve(0), bool()) {
      return true;
    }
    
    // Used as fallback when SFINAE culls the template method
    constexpr bool has_reserve_method(...) { return false; }
    

    You can then use this in a class for example:

    template <typename T, bool b>
    struct Reserver {
      static void apply(T& t, size_t n) { t.reserve(n); }
    };
    
    template <typename T>
    struct Reserver <T, false> {
      static void apply(T& t, size_t n) {}
    };
    

    And you use it so:

    template <typename T>
    bool reserve(T& t, size_t n) {
      Reserver<T, has_reserve_method(t)>::apply(t, n);
      return has_reserve_method(t);
    }
    

    Or you can choose a enable_if method:

    template <typename T>
    auto reserve(T& t, size_t n) -> typename std::enable_if<has_reserve_method(t), bool>::type {
      t.reserve(n);
      return true;
    }
    
    template <typename T>
    auto reserve(T& t, size_t n) -> typename std::enable_if<not has_reserve_method(t), bool>::type {
      return false;
    }
    

    Note that this switching things is actually not so easy. In general, it’s much easier when just SFINAE exist — and you just want to enable_if one method and not provide any fallback:

    template <typename T>
    auto reserve(T& t, size_t n) -> decltype(t.reserve(n), void()) {
      t.reserve(n);
    }
    

    If substitution fails, this method is removed from the list of possible overloads.

    Note: thanks to the semantics of , (the comma operator) you can chain multiple expressions in decltype and only the last actually decides the type. Handy to check multiple operations.

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

Sidebar

Related Questions

First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
Kindly go through this link and notice the solution given by "thepeer": https://stackoverflow.com/a/3494108 I
Duplicate : https://stackoverflow.com/questions/135651/learning-unit-testing I'm trying to develop some software for my research group to
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
In answering this question ( https://stackoverflow.com/questions/352317/c-coding-question#352327 ), it got me wondering... Is there any
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me class Form { protected $inputs = array(); public function
I was reading this thread/post: https://stackoverflow.com/questions/262298/windows-c-ui-technology and am also wondering about a non .NET
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
This question is similar in spirit to : https://stackoverflow.com/questions/492178/links-between-personality-types-and-language-technology-preferences But it is based specifically

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.