Named int vs named float
From here I've got the mixed feelings that something is not right. 开发者_Go百科
Is it true that named int isn't lvalue but named float is?
The comment in the link says "integer, pointer and member pointer template parameters aren’t lvalues" (emphasis mine). It does not say that named integer variables are not lvalues - they are.
If there were such a thing as floating-point template parameters, then they wouldn't be lvalues either; but named floating-point variables still would be.
That's specifically related to using an int as a template parameter being an rvalue, while you can't use floats as template arguments.
eg.
template <int T>
struct foo
{
void f(int&);
void f(int&&);
void bar()
{
int x;
f(x); // calls first version
f(T); // calls second version, despite being "named"
}
};
template <float F> // compile error: can't use a float as a template parameter
struct bad {};
What do you mean by "named value"? There isn't such a concept in C++. A variable (regardless of its type) is an lvalue (when used in an expression). In most cases, things with reference types are lvalues, things other than variables with data types aren't (but I'm sure someone will find some exceptions); again, the data type (integer or floating point) has nothing to do with it.
The thread in question is discussing non-type template parameters. The above rule applies here as well: references are lvalues, other types are not lvalues. The confusion seems to occur because only a very limited set of non-reference types can be used as non-type template parameters: in particular, integral types are OK, but floating point types aren't. So within a template, a floating point argument must be a reference (and thus an lvalue), an integral type can be either a value type (not an lvalue) or a reference (an lvalue), e.g.:
template <int N>
struct A { /* N is not an lvalue */ };
template<int& N>
struct B { /* N is an lvalue */ };
template <double N>
struct C {}; // This is illegal, and shouldn't compile
template <double& N>
struct D { /* N is an lvalue */ };
The difference here isn't whether N
has integral type or not; it's
whether N
is a reference or not.
精彩评论