(I know this is very similar to some other questions on here, but I haven't found any that specifically, language-lawyerly, answer this precise detail. Most of the near-duplicates are just asking whether they should use static_cast
over functional-style casts (answer: yes), or the difference between static_cast
and C-style casts.)
In C++, the following two casts appear to be very similar:
template<class T, class U> auto convert1(U&& u) {
return T( std::forward<U>(u) );
}
template<class T, class U> auto convert2(U&& u) {
return static_cast<T>( std::forward<U>(u) );
}
Is there any difference for any types T
, U
; or are they 100% identical in effect?
If they are different, I'd appreciate some examples of places in the Standard Library where the subtle difference is relevant. (I mean like how std::make_shared<T>(args...)
is specified to construct its object with T(args...)
instead of T{args...}
because of the subtle difference between T()
and T{}
there.)