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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:45:22+00:00 2026-05-22T21:45:22+00:00

I have a question about templates that can be used with parameters that are

  • 0

I have a question about templates that can be used with parameters that are either a class or a primitive type. Here’s some sample code:

(Note: I have real code that’s more complex, the following code is useless but it reproduces the same issue)

template<typename T>
class Foo
{
  T value;

public:
  Foo() {}
  const T& getValue() const { return value; }
  Foo& setValue(const T& other) { 
    value = other; return *this; 
  }
};

struct Bar
{
  int x;

  Bar() : x(3) {}
};

int doit()
{
  Foo<int> fooint;
  Bar bar;
  bar.x = 44;
  Foo<Bar> foobar;

  fooint.setValue(3);      // warning here
  foobar.setValue(bar);

  int y = foobar.getValue().x + fooint.getValue();
  return y;
}

I get a compiler remark on fooint.setValue():

value copied to temporary, reference to temporary used

I understand the remark. What I’m wondering is how I should handle Foo::setValue() if I’m going to use Foo with both primitives and class/struct types as template parameters.

I thought setValue(const T& other) was the right method signature for passing in a constant class by reference.

Is there a way to make setValue() so it “does the right thing” both for Foo<int> and Foo<Bar>?

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

    It is perfectly legal to bind temporaries to const references as you do in setValue(). Intel C++, which issues this remark, is not being helpful in this case.

    EDIT: I am guessing that TI compiler is based on Intel, which, for me, issues the following diagnostic on that line:

    test.cc(28): remark #383: value copied to temporary, reference to temporary used
        fooint.setValue(3);      // warning here
    

    The diagnostic is discussed on http://software.intel.com/en-us/articles/cdiag383/ where it says

    Can safely ignore this warning for pushback function of vector. The vector copies the argument into its own storage; it never stores the original argument. Therefore, using a temporary is perfectly safe.

    In your case, you’re also copying the argument, therefore it can also be ignored.

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

Sidebar

Related Questions

I have a question about templates. I would like to have a templated class
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have a question about tables in MySQL. I'm currently making a website where

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.