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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:12:54+00:00 2026-06-13T07:12:54+00:00

consider the code template <class A> class B; template <class A> class B<const A>{};

  • 0

consider the code

     template <class A>
     class B;

     template <class A>
     class B<const A>{};

     template <class A, int N>
     class B<A[N]>{};

     template <class A>
     class B<A*>{};

     template <class A>
     class B<A&>{};

The following template instantiations work fine:

     A<int*&>
     A<const int*>
     A<int*[3]>

but the following one doesn’t work:

     A<const int[3]>

Is there some reason that this particular combination is invalid or is it perhaps a bug with g++4.6.3?

By the way I managed to get around this using SFINAE and boost::disable_if<>, so at least the problem is solved.

EDIT

I forgot to mention that the error in question is an ambiguous class template instantiation and it couldn’t decide between the overload for const or the overload for an array.

EDIT2

This has nothing to do with pointers, here’s the full context:

I’m going through the book C++ Template Metaprogramming and am doing question 2-3 (Chapter 2 question 3) which says:

Use the type traits facilities to implement a type_descriptor class template, whose instances, when streamed, print the type of their template parameters:
NOTE: we cannot use RTTI to the same effect since, according to 18.5.1 [lib.type.info] paragraph 7 of the standard, typeid(T).name() is not guaranteed to return a meaningful result.

My solution (including the the workaround for the compilation error) is as follows:

    //QUESTION 2-3
    template <class T, class enable = void>
    struct type_descriptor
    {
      std::string operator()() const
      {
        return "Unknown";
      }
    };

    //specializations for primitive types
    #define TYPE_DESC_SPEC(type) template <>    \
      struct type_descriptor<type,void>     \
      {std::string operator()() const{return #type;}};

    TYPE_DESC_SPEC(int)
    TYPE_DESC_SPEC(long)
    TYPE_DESC_SPEC(void)
    TYPE_DESC_SPEC(short)
    TYPE_DESC_SPEC(unsigned char)
    TYPE_DESC_SPEC(unsigned short)
    TYPE_DESC_SPEC(unsigned long)

    //specializations for modifiers *, const, &, and [N]

    template <class T>
    struct type_descriptor<T&,void>
    {std::string operator()(){return type_descriptor<T>()() + " &";}};

    template <class T>
    struct type_descriptor<T*,void>
    {std::string operator()(){return type_descriptor<T>()() + " *";}};

    //Replace void with what's in the comment for the workaround.
    template <class T>
    struct type_descriptor<const T, void/*typename boost::disable_if<boost::is_array<T> >::type*/>
    {std::string operator()(){return type_descriptor<T>()() + " const";}};

    template <class T>
    struct type_descriptor<T(*)(),void>
    {std::string operator()(){return type_descriptor<T>()() + " (*)()";}};

    template <class T, class U>
    struct type_descriptor<T(*)(U),void>
    {std::string operator()(){return type_descriptor<T>()() + " (*)(" + type_descriptor<U>()() + ")";}};

    template <class T, int N>
    struct type_descriptor<T[N],void>
    {
      std::string operator()()
      {
        std::stringstream s;
        s << type_descriptor<T>()() << " [" << N << "]";
        return s.str();
      }
    };

    template <class T>
    struct type_descriptor<T[],void>
    {std::string operator()(){return type_descriptor<T>()() + " []";}};

    //Now overload operator<< to allow streaming of this class directly

    template <class T>
    std::ostream & operator<<(std::ostream & s, type_descriptor<T> t)
    {
      return s << t();
    }
    //END QUESTION 2-3

Sample usage is:

      std::cout << "\nQuestion 2-3 results\n";
      std::cout << type_descriptor<int*>() << std::endl;
      std::cout << type_descriptor<int*[3]>() << std::endl;
      std::cout << type_descriptor<std::string*>() << std::endl;
      std::cout << type_descriptor<const int&>() << std::endl;
      std::cout << type_descriptor<const int *const&>() << std::endl;
      std::cout << type_descriptor<int[4]>() << std::endl;
      std::cout << type_descriptor<int(*)()>() << std::endl;
      std::cout << type_descriptor<int*&(*)(const char &)>() << std::endl;
      std::cout << type_descriptor<int*&>() << std::endl;
      std::cout << type_descriptor<int[]>() << std::endl;
      std::cout << type_descriptor<const long[]>() << std::endl;

and the corresponding output is (when the workaround is in, otherwise it doesn’t compile on that last one):

int *

int * [3]

Unknown *

int const &

int const * const &

int [4]

int (*)()

int * & (*)(Unknown const &)

int * &

int []

long const []

So C++ is able to differentiate pointers and arrays for the template parameters, is able to correctly, recursively, separate compound types and output the correct result, except for const A[]. It needs help with that one

  • 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-13T07:12:55+00:00Added an answer on June 13, 2026 at 7:12 am

    An array type with a const element type is both a const qualified type (the const applies bidirectionally) and an array type.

    So you should fix the specializations.

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

Sidebar

Related Questions

Consider the following code : template<typename T> class Base { Base(); Base(const Base<T>& rhs);
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Consider the following code: class MyClass { template <typename Datatype> friend MyClass& operator<<(MyClass& MyClassReference,
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
Consider the following code: template<bool> class StaticAssert; template<> class StaticAssert<true> {}; StaticAssert< (-1 <
Consider the following simplified version of my code. I have a template class A
Consider the following code: template<class T, class F> struct X {}; template<class T, class
Consider the following code: template <typename T> class B { }; template <typename T>
Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)

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.