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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:49:47+00:00 2026-05-25T21:49:47+00:00

I have a problem where I’d like to provide a generic version of a

  • 0

I have a problem where I’d like to provide a generic version of a function foo which may only be applied when there is absolutely no other match for an invocation. How can I modify the following code such that last_resort::foo is a worse match for derived::type than base::foo? I’d like to find a solution which does not involve modifying the definition of bar and which would preserve the type of the argument of last_resort::foo.

#include <iostream>

namespace last_resort
{

template<typename T> void foo(T)
{
  std::cout << "last_resort::foo" << std::endl;
}

}

template<typename T> void bar(T)
{
  using last_resort::foo;
  foo(T());
}

namespace unrelated
{

struct type {};

}

namespace base
{

struct type {};

void foo(type)
{
  std::cout << "base::foo" << std::endl;
}

}

namespace derived
{

struct type : base::type {};

}

int main()
{
  bar(unrelated::type()); // calls last_resort::foo
  bar(base::type());      // calls base::foo
  bar(derived::type());   // should call base::foo, but calls last_resort::foo instead

  return 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-05-25T21:49:47+00:00Added an answer on May 25, 2026 at 9:49 pm

    last_resort::foo can be removed from the overload set with disable_if. The idea is to disable last_resort::foo(T) if foo(T) is otherwise well-formed. This causes last_resort::foo(T) to be the worst match for foo:

    namespace test
    {
    
    template<typename T> struct has_foo { ... };
    
    }
    
    namespace last_resort
    {
    
    template<typename T>
      struct disable_if_has_foo
        : std::enable_if<
            !test::has_foo<T>::value
          >
    {};
    
    template<typename T>
      typename disable_if_has_foo<T>::type foo(T)
    {
      std::cout << "last_resort::foo" << std::endl;
    }
    
    }
    

    The output:

    $ g++ last_resort.cpp 
    $ ./a.out 
    last_resort::foo
    base::foo
    base::foo
    

    This answer describes how to build a solution for checking for the existence of a function (foo) returning void.

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

Sidebar

Related Questions

I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I have problem with fancybox. I want to write a function that will run
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have problem with code. I have written a function for extracting a parameter,
i have problem with jquery submit and post. I would like to submit a
I have problem with php's preg_match.I want to make something like this : When
I have problem in my split Sentence function . The idea of ​​my function
I have problem with Joomla layouts in my component..There must be something bad with
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:
I have problem on deleting component which is created on runtime. Please help me.

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.