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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:56:30+00:00 2026-05-26T11:56:30+00:00

lately I’ve been toying around with templates and stumbled upon the following problem. I

  • 0

lately I’ve been toying around with templates and stumbled upon the following problem. I am implementing the CRTP pattern like this:

template<typename derived_t>
struct protocol_object
{
    ...
};

struct data_object : public protocol_object<data_object>
{
    ...
};

I now would like to match instances of class protocol_object in a member template function, while still accepting non CRTP-types:

struct consumer_impl
{
    template<typename derived_t>
    void match(protocol_object<derived_t> &value)
    {
       std::cout << "protocol_class";
    };

    template<typename T>
    void match(T &value)
    {
       std::cout << "any other type";
    };
}

Unfortunately only the second version is ever called. Apparently match(protocol_object<derived_t> &value) is not considered or rejected in favour of the more general form match(T &value).

data_object object;
double value;
consumer_impl consumer;

consumer.match(value);  // yields "any other type" OK
consumer.match(object); // also yields "any other type" but want "protocol_class"

Is there a way out of this?

Thanks for any hints.
Arne

  • 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-26T11:56:30+00:00Added an answer on May 26, 2026 at 11:56 am

    This isn’t related to CRTP. It’s a general case of the following:

    • Design a template function, such that all derived classes use a particular specialization.

    The issue is that T& value is an exact match for Derived&, while Base& is an inexact match. So we shall make the general form a worse match:

    struct conversion_required { conversion_required(int) {} };
    
    template<typename derived_t>
    void match_impl(protocol_object<derived_t> &value, int)
    {
       std::cout << "protocol_class";
    };
    
    template<typename T>
    void match_impl(T &value, conversion_required)
    {
       std::cout << "any other type";
    };
    
    template<typename T>
    void match(T& value)
    {
        return match_impl(value, 0);
    }
    

    Now the specialization, requiring an upcast, is a better match than the general template, requiring a user-defined conversion.

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

Sidebar

Related Questions

Lately, we've been seeing exceptions like this in our .NET (.asmx) webservices: System.Web.Services.Protocols.SoapException: Server
Lately I've been working on implementing security for my web application, running on a
Lately, I have been doing this in an effort to speed up performance (and
Lately whenever I do a git push I get an exchange like this: owner-pc
Lately i've been through a lot of times on a single situation problem: I
Lately I have been trying my hands on Eclipse IDE for java development. I
Lately I've been favoring using named pipes (option --enable-named-pipes) in MySQL running on windows,
Lately, I have taken to the pattern of having a lot of diagnostic logging
Lately, I'v been struggling with an annoying situation on ASP.NET MVC. Here's the story
Lately I've been thinking a lot about building a website/blog/community oriented site. However 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.