Is this std :: decay implementation correct

Is this std :: decay implementation correct?

template<class T> T DecayType(T); template<class T> struct decay { using type = decltype(DecayType(declval<T>())); }; 

I ask because everything I came across uses some branching pattern to carefully manipulate the type, while this seems to work by definition.

+5
source share
2 answers

Forming a function call like this requires passing by value, which requires the copy / move constructor. This implementation is not general enough.

This is the essence of what std::decay does.

+6
source

No, this is not correct, for the reasons that Potatoswatter gave. In addition to requiring the copy / move constructor to return by value, you cannot return some types by value at all:

 #include <type_traits> template<class T> T DecayType(T); template<class T> struct decay { using type = decltype(DecayType(std::declval<T>())); }; struct abstract { virtual void f() = 0; }; static_assert(std::is_same<decay<abstract&>::type, abstract>::value, ""); struct incomplete; static_assert(std::is_same<decay<incomplete&>::type, incomplete>::value, ""); struct immortal { ~immortal() = delete; }; static_assert(std::is_same<decay<immortal&>::type, immortal>::value, ""); 
+4
source

All Articles