Namespaces
Variants
Views
Actions

Talk:cpp/utility/optional/optional

From cppreference.com
< Talk:cpp‎ | utility‎ | optional
Revision as of 18:51, 2 March 2017 by Gubbins (Talk | contribs)

template < class U = T >
/* EXPLICIT */ constexpr optional( U&& value );
(8) (since C++17)

This doesn't make sense to me. Should it be template < class T = U >?

gubbins (talk) 02:15, 25 January 2017 (PST)

T is the template parameter of std::optional. --D41D8CD98F (talk) 02:40, 25 January 2017 (PST)
We could replace T with value_type (or even with std::optional<T>::value_type), but I guess that wouldn't make it clearer. --D41D8CD98F (talk) 02:42, 25 January 2017 (PST)
Why not just say template < class U >? I can't really see what the = T part adds except for confusion. It would be better to describe the behaviour in the full constructor descriptions - like constructor 3 for std::pair, for example. gubbins (talk) 13:42, 25 January 2017 (PST)
template < class U > would be wrong because it would be a rather different constructor. template <class U = T> EXPLICIT constexpr optional(U&& v); was added to the standard by LWG 2756 which describes its purpose. if you believe it is not useful.. https://isocpp.org/std/submit-issue --Cubbi (talk) 14:00, 25 January 2017 (PST)
A note may help, though. A more radical idea is a global stylistic change to say std::optional<T>::optional in the title instead (i.e., include the template parameters), so that it's clear what T is. T. Canens (talk) 19:16, 26 January 2017 (PST)
I agree with the remark above. The document you linked does not refer to template <class U = T> in isolation; it is within a declaration of template <class T> optional. So in that setting, the meaning of T is explicit. To readers of this page I do not think it is very clear that T is the template parameter of the optional template itself. gubbins (talk) 18:51, 2 March 2017 (PST)