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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:07:30+00:00 2026-05-13T19:07:30+00:00

This question is related to my last one . I am trying to solve

  • 0

This question is related to my last one. I am trying to solve the problem using traits<T> and traits<T*>. Please consider the following code.

template<typename T>
struct traits
{
    typedef const T& const_reference;
};

template<typename T>
struct traits<T*>
{
    typedef const T const_reference;
};

template<typename T>
class test
{
public:   
    typedef typename traits<T>::const_reference const_reference;
    test() {}   
    const_reference value() const {
        return f;
    }
private:
    T f;
};

int main()
{
    const test<foo*> t;
    const foo* f = t.value(); // error here. cannot convert ‘const foo’ to ‘const foo*’ in initialization
    return 0;
}

So it looks like compiler is not considering the traits specialization for pointers and taking return type of value() as const foo rather than const foo*. What am I doing wrong here?

Any help would be great!

  • 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-13T19:07:31+00:00Added an answer on May 13, 2026 at 7:07 pm

    The specialization is being used. traits<foo*>::const_reference is const foo. If you want it to be a pointer, use:

    template<typename T>
    struct traits<T*>
    {
        typedef const T* const_reference;
    };
    

    With this, traits<foo*>::const_reference will be const foo*.

    Note that the use of T in the traits<T*> specialization is completely separate from the T in the traits template. You could rename it:

    template<typename U>
    struct traits<U*>
    {
        typedef const U* const_reference;
    };
    

    and you’ll have the same specialization. It makes more sense if you’ve experience in functional programming.

    To start, think of the template <typename ...> as introducing an abstraction, rather like a function abstracts out a value. It’s like turning

    sum = 0
    for item in [1,2,3]:
        sum += item
    

    into:

    function sum(l):
        sum = 0
        for item in l:
            sum += item
        return sum
    

    where l takes the place of [1,2,3]. We can call sums from another function that itself has a formal parameter named l:

    function sumsq(l):
        return sum(map(lambda x: x*x, l))
    

    sumsq‘s “l” has nothing to do with sum‘s “l”.

    With templates, we abstract type names rather than values. That is, we turn:

    struct traits {
        typedef const double& const_reference;
    };
    

    into:

    template <typename T>
    struct traits {
        typedef const T& const_reference;
    };
    

    Now consider a non-template specialization:

    template <>
    struct traits<double*> {
        typedef const double* const_reference;
    };
    

    Here there are no template parameters for the specialization, but you can think of traits<double*> as applying a traits template to a double*. Abstract out the double and you have:

    template <typename T>
    struct traits<T*> {
        typedef const T* const_reference;
    };
    

    Here the T is a parameter for the specialization, not the base template.

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

Sidebar

Related Questions

This question is related to the one I asked here . I'm trying to
This question is related to my last one about jQuery 1.4. They supposedly fixed
Please note this question related to performance only. Lets skip design guidelines, philosophy, compatibility,
This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to this SO post Rather than using a recursive CTE
This question is related somewhat to the one i asked HERE . Now, i
Background This question is related to Why does String.valueOf(null) throw a NullPointerException? Consider the
I hope this question is considered programing related. I've been learning and using .NET
I'm not sure if my question is related to this one or not. IE9
This is related, but not identical to my last question . So I thought

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.