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

The Archive Base Latest Questions

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

Please consider the following code. struct foo { }; template<typename T> class test {

  • 0

Please consider the following code.

struct foo
{
};

template<typename T>
class test
{
public:   

    test() {} 

    const T& value() const
    {
        return f;
    }

private:
    T f;
};


int main()
{
    const test<foo*> t;
    foo* f = t.value();
    return 0;
}

t is a const variable and value() is a constant member-function which returns const T&. AFAIK, a const type is not assignable to a non-const type. But how foo* f = t.value(); compiles well. How this is happening and how can I ensure value() can be only assigned to const foo*?

Edit

I found that, this is happening on when templates are used. Following code works as expected.

class test
{
public:   

    test() {} 

    const foo* value() const { return f; }

private:
    foo* f;
};


int main()
{
    const test t;
    foo* f = t.value(); // error here
    return 0;
}

Why the problem is happening when templates are used?

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

    Because you have two levels of indirection – in your main function, that call to value returns a reference to a const pointer to a non-const foo.

    This can safely be copied into non-const pointer to a non-const foo.

    If you’d instantiated test with const foo *, it would be a different story.

    const test<const foo*> t;
    foo* f = t.value(); // error
    const foo* f = t.value(); // fine
    return 0;
    

    Update

    From the comment:

    value() returns const T& which can
    only be assigned to another const
    type. But in this case, compiler is
    safely allowing the conversion.

    Const data can only be read. It cannot be written (“mutated”). But copying some data is a way of reading it, so it’s okay. For example:

    const int c = 5;
    int n = c;
    

    Here, I had some const data in c, and I copied the data into a non-const variable n. That’s fine, it’s just reading the data. The value in c has not been modified.

    Now, suppose your foo had some data in it:

    struct foo { int n; };
    

    If I have a non-const pointer to one of those, I can modify the n value through the pointer. You asked your test template to store a pointer to a non-const foo, and then made a const instance of test. Only the pointer address is constant, therefore. No one can change the address stored in the pointer inside test, so it cannot be made to point to another object. However, the object it points to can have its contents modified.

    Update 2:

    When you made your non-template version of the example, you made a mistake. To get it right, you need to substitute foo * into each place where there’s a T.

    const T& value() const
    

    Notice that you have a reference to a const T there. So the return value will be a reference to something const: a foo *. It’s only the pointer address that can’t be modified. The object it points to can have its contents modified.

    In your second example, you got rid of the reference part, which changes the meaning and makes the const modifier apply to the object that the pointer points to, instead of applying to the pointer itself.

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

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ruby's sort doesn't sort in-place. (Do you have a Python… May 15, 2026 at 4:00 pm
  • Editorial Team
    Editorial Team added an answer In your form submit handler, use preventDefault() to stop the… May 15, 2026 at 4:00 pm
  • Editorial Team
    Editorial Team added an answer Try hooking into the afterShowSearch event, where you would update… May 15, 2026 at 4:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.