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>?
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:
The diagnostic is discussed on http://software.intel.com/en-us/articles/cdiag383/ where it says
In your case, you’re also copying the argument, therefore it can also be ignored.