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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:38:12+00:00 2026-06-12T21:38:12+00:00

I have been reading through C++ Template Metaprogramming and doing the exercises contained therein

  • 0

I have been reading through C++ Template Metaprogramming and doing the exercises contained therein and have come across a problem. The core of the problem can be seen with this small example:

    template <class A, class B>
    struct C;

    template <class A>
    struct C<A,A>
    {
      typedef some_type type;
    };

    template <class A, class B>
    struct C<A*,B>
    {
      typedef some_other_type type;
    };

    int main()
    {
      C<int*,int*> c;
    }

This fails to compile because c’s type is ambiguous. The compiler can’t tell which specialization it’s suppose to instantiate, however, I know that in a case like this I want the first specialization to be called. The solution I’ve come up with so far is to rewrite it like this

    template <class A, class B>
    struct C;

    template <class A, class B>
    struct C<A*,B>
    {
      typedef typename boost::mpl::if_c<boost::is_same<A*,B>::value,
                                        some_type, some_other_type>::type type;
    };

The problem with this solution is that I actually have partial specializations for each of pointer, const, reference and array types, so I’ve had to add this check to each specialization individually.

My question is, then, is there some way to have the nice compact specializations of before and somehow tweak it so that the compiler instantiates the specialization

     template <class A>
     struct C<A,A>
     {
       typedef some_type type;
     };

instead of any of the other specializations in case of ambiguity?

For those interested the question I’m working on is Chapter 2 question 1.

  • 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-12T21:38:13+00:00Added an answer on June 12, 2026 at 9:38 pm

    You may add another parameter to your base template and use it to enable/disable your specialization. For example you may have:

    template< class A, class B, class Enable = void >
    struct C;
    template< class A >
    struct C<A, A, void> {...};
    template< class A, class B >
    struct C<A*, B, typename boost::disable_if<boost::is_same<A*, B> >::type> {...};
    

    Now if you want to handle const pointer or any thing else you may extend your condition according to type traits(is_const, is_pointer, …)!

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

Sidebar

Related Questions

I've been reading through a number of Emacs Lisp packages and have come across
I have been reading through the core data documentation and feel I am still
I can’t really understand how YouTube serves videos, but I have been reading through
I have been reading through this wonderful website regarding the recommended Python IDEs and
I have been reading through the documentation on the JavaScriptMVC framework and it looks
I have been reading through several websites and even used NVIDA's code as a
I've been reading through a nice tutorial and have a quick question concerning something
I have been reading through this similar question and find myself less than satisfied
So I've been reading through the K&R C book and have a question.. in
I have been reading through lots of Q&A everywhere and these stackoverflow posts seem

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.