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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:48:01+00:00 2026-05-31T04:48:01+00:00

I am currently trying to get the following to compile: class foo { };

  • 0

I am currently trying to get the following to compile:

class foo {
};

class bar {
public:
  const foo & to_foo() const {
    return f;
  }

  foo & to_foo() {
    return f;
  }
private:
 foo f;
};

template< typename T, typename Enable = void >
class convert {};

template< typename T >
struct convert< T, typename std::enable_if< std::is_member_function_pointer< decltype( &T::to_foo ) >::value >::type > {

  static const foo & call1( const bar & b ) {
    return b.to_foo();
  }

  static foo & call2( bar & b ) {
    return b.to_foo();
  }
};

However the specialisation get’s confused by the presence of two possible to_foo() members, so it will choose the default case. As soon as I remove one of the to_foo() members, it works, but then one of the callX() methods fails because it does not match the constness.

Is there any way to detect this function in this case?

EDIT:

Here is an example on ideone: http://ideone.com/E6saX

When one of the the methods is removed, it works just fine: http://ideone.com/iBKoN

  • 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-31T04:48:03+00:00Added an answer on May 31, 2026 at 4:48 am

    It is still a bit unclear to me what you are trying to achieve. I will suppose that the target type (foo) is fixed and we are not attempting to create a full bridge system.

    In this case, we can ditch the structure and just rely on overload selection.

    foo const& to_foo(bar const& b) { return b.to_foo(); }
    foo& to_foo(bar& b) { return b.to_foo(); }
    

    Works just fine, as far as the actual translation goes. No template involved.

    Now the question might be how to actually detect whether this conversion is possible or not. In this case, we need to use SFINAE to avoid a hard-error while attempting the conversion.

    #include <iostream>
    #include <utility>
    
    // Didn't remember where this is implemented, oh well
    template <typename T, typename U> struct same_type: std::false_type {};
    template <typename T> struct same_type<T, T>: std::true_type {};
    
    // Types to play with
    struct Foo {};
    struct Bar { Foo _foo; };
    struct Bad {};
    
    Foo const& to_foo(Bar const& b) { return b._foo; }
    Foo& to_foo(Bar& b) { return b._foo; }
    
    // Checker
    template <typename T>
    struct ToFoo {
      T const& _crt;
      T& _rt;
    
      template <typename U>
      static auto to_foo_exists(U const& crt, U& rt) ->
          decltype(to_foo(crt), to_foo(rt), std::true_type());
    
      static std::false_type to_foo_exists(...);
    
      // Work around as the following does not seem to work
      // static bool const value = decltype(to_foo_exists(_crt, _rt))::value;
      static bool const value = same_type<
                                    decltype(to_foo_exists(_crt, _rt)),
                                    std::true_type
                                >::value;
    };
    
    // Proof
    int main() {
      std::cout << ToFoo<Bar>::value << "\n"; // true
      std::cout << ToFoo<Bad>::value << "\n"; // false
    }
    

    Note: successfully compiled on Clang 3.0 (with the work around) and gcc 4.5.1.

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

Sidebar

Related Questions

I'm trying to compile code in g++ and I get the following errors: In
I'm currently stuck trying to get my objective c generated files to compile in
I'm currently trying to get into the Java EE development with the Spring framework.
I'm currently trying to get a UISlider to work. I'm trying to set the
I'm currently trying to get the output of an executable console-app into an other
I am currently trying to get data out of a plist. It basically looks
I'm currently trying to get a grip on Android development and working my way
i am currently trying to get Input Mode Editor (IME) support working with a
Heya, I'm currently trying to get bltoolkit working in my project. I've added the
I am trying to get MSTest working with CruiseControl.Net. I currently have it working

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.