Return value without declaration of return type in the template, is this a typo?

I watch a conversation. Modern metaprogramming of Walter E. Brown's templates . At 54:40, the code is shown below.

template<class T, T v> struct integral_constant{ static constexpr T value = v; constexpr operator T() const noexcept { return value; } // what does this mean? constexpr T operator T() const noexcept { return value; } }; 

My question is what this line means constexpr operator T() const noexcept { return value; } constexpr operator T() const noexcept { return value; } , why there is no return type, but it still returns value ? This is a missprint?

+6
source share
2 answers

Yes, the second line of the statement is incorrect and can be completely deleted.

A type operator, for example, for example. operator int() executed
when an object is flooded or implicitly converted to type:

 MyClass myObject; int i = myObject; // here operator int() is used. 

Naturally, operator int() should return int . For such operators, it is not required or allowed to write a specific type of return. In your case, this is not an int of float or something specific, but a template type, but it is the same idea.

In addition to the problem with the return type, the second line of the operator again defines the same operator with the same parameters, there cannot be several functions with the same names and parameters.

And after a whole struct no semicolons.

After fixing these issues, it compiles: http://ideone.com/Hvrex5

+9
source

The first is not a typo. This syntax is used to provide conversion from a class object to another type.

Return Type T

See http://en.cppreference.com/w/cpp/language/cast_operator for details.

The consexpr tells the compiler that the return value of a member function can be determined at compile time if the object to which it is called is also constexpr qualified.

The second is not a legal expression.

+3
source

All Articles