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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:47:06+00:00 2026-06-02T19:47:06+00:00

Having trouble with SFINAE. I need to be able to determine if a Type

  • 0

Having trouble with SFINAE. I need to be able to determine if a Type has a member function operator-> defined regardless of its return type. Example follows.

This class in the tester. It defines operator->() with a return type of X*. I thus will not know what ‘X’ is to hardcode it everywhere.

template <class X>
class PointerX
{
    ...

    X* operator->() const;
    ...
}

This class tries to determines if the passed in T has a method operator-> defined; regardless of what operator-> return type is.

template<typename T>
struct HasOperatorMemberAccessor
{
    template <typename R, typename C> static R GetReturnType( R ( C::*)()const);

    template<typename U, typename R, R(U::*)()const> struct SFINAE{};
    template<typename U> static char Test(SFINAE<U,     decltype( GetReturnType(&U::operator->)),   &U::operator-> >*);
    template<typename U> static uint Test(...);
    static const bool value = sizeof(Test<T>(0)) == sizeof(char);
};

This class is the exact same as above except that operator-> return type has to be ‘Object’.

template<typename T>
struct HasOperatorMemberAccessorOBJECT
{
    template <typename R, typename C> static R GetReturnType( R ( C::*)()const);

    template<typename U, typename R, R(U::*)()const> struct SFINAE{};
    template<typename U> static char Test(SFINAE<U,     Object*,                &U::operator-> >*); // only change is we hardcoded Object as return type.
    template<typename U> static uint Test(...);
    static const bool value = sizeof(Test<T>(0)) == sizeof(char);
};

Results:

void main()
{
    HasOperatorMemberAccessor<PointerX<Object>>::Test<PointerX<Object>>(0);         // fails  ::value is false;  Test => Test(...)

    HasOperatorMemberAccessorOBJECT<PointerX<Object>>::Test<PointerX<Object>>(0);       // works! ::value is true;   Test => Test(SFINAE<>*)  
}

HasOperatorMemberAccessor was unable to find PointX’s member function “Object operator->() const”.
So it uses Test’s generic version Test(…).

However, HasOperatorMemberAccessorOBJECT was able to find PointX’s “Object operator->() const”.
Thus it uses Test specialized version Test(SFINAE*).

Both should have been able to find the “Object operator->() const” method; and thus both should use Test’s specialized version Test(SFINAE*); and thus HasOperatorMemberAccessor>::value should be true for both.

The only difference between HasOperatorMemberAccessor and HasOperatorMemberAccessorOBJECT is that HasOperatorMemberAccessorOBJECT has the typename R hardcoded to object,

So the issue is that “decltype( GetReturnType(&U::operator->))” is not returning Object correctly. I’ve tried a number of different permitations of discovering the return type. They go as follows:

    decltype( GetReturnType(&U::operator->) )
    typename decltype( GetReturnType(&U::operator->))
    decltype( ((U*)nullptr)->operator->() )
    typename decltype( ((U*)nullptr)->operator->() )

None work, why? I’m using MSVC++ 10.0.

  • 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-02T19:47:08+00:00Added an answer on June 2, 2026 at 7:47 pm

    Are you asking how to implement such a trait, or why decltype isn’t behaving as you expect? If the former, here’s one approach:

    #include <type_traits>
    
    template<typename T, bool DisableB = std::is_fundamental<T>::value>
    struct HasOperatorMemberAccessor
    { 
    private:
        typedef char no;
        struct yes { no m[2]; };
    
        struct ambiguator { char* operator ->() { return nullptr; } };
        struct combined : T, ambiguator { };
        static combined* make();
    
        template<typename U, U> struct check_impl;
        template<typename U>
        static no check(
            U*,
            check_impl<char* (ambiguator::*)(), &U::operator ->>* = nullptr
        );
        static yes check(...);
    
    public:
        static bool const value=std::is_same<decltype(check(make())), yes>::value;
    };
    
    // false for fundamental types, else the definition of combined will fail
    template<typename T>
    struct HasOperatorMemberAccessor<T, true> : std::false_type { };
    
    // true for non-void pointers
    template<typename T>
    struct HasOperatorMemberAccessor<T*, false> :
        std::integral_constant<
            bool,
            !std::is_same<typename std::remove_cv<T>::type, void>::value
        >
    { };
    
    template<typename X>
    struct PointerX
    {
        X* operator ->() const { return nullptr; }
    };
    
    struct X { };
    
    int main()
    {
        static_assert(
            HasOperatorMemberAccessor<PointerX<bool>>::value,
            "PointerX<> has operator->"
        );
        static_assert(
            !HasOperatorMemberAccessor<X>::value,
            "X has no operator->"
        );
        static_assert(
            HasOperatorMemberAccessor<int*>::value,
            "int* is dereferencable"
        );
        static_assert(
            !HasOperatorMemberAccessor<int>::value,
            "int is not dereferencable"
        );
        static_assert(
            !HasOperatorMemberAccessor<void*>::value,
            "void* is not dereferencable"
        );
    }
    

    VC++ 2010 lacks the necessary C++11 facilities (e.g. expression SFINAE) needed to make this much cleaner.

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

Sidebar

Related Questions

Having trouble with a mysql grant statement. I want a user who has: readonly
Having trouble where I think I need to provide most of my code to
Having trouble with a function hitting the page as soon as possible, so i
Having trouble geting the encoding right in my silverlight application. I need support for
Having trouble with a query to return the newest order of any grouped set
Having trouble finding a solution for my situation here. Sorry if this has been
Having trouble inheriting from a template class. Looks something like this: template<typename type> class
Having trouble with this jQuery function: $(#content).siblings().each(function(i){ heightOfSiblings = heightOfSiblings + this.outerHeight(); }); Error
Having trouble with iterator. It need to iterate over ArrayList and find any other
Having trouble with query results. The getSales() function works great the first time it

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.