簡體   English   中英

這個std :: decay的實現是否正確

[英]Is this implementation of std::decay a correct one

這個std :: decay的實現是正確的嗎?

template<class T>
T DecayType(T);

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

我問,因為我遇到的所有內容都使用了一些模板分支來仔細操作類型,而這似乎只是按照定義行事。

形成這樣的函數調用需要傳遞值,這需要復制/移動構造函數。 這種實現不夠通用。

不過,這是std::decay的主旨。

不,這是不正確的,因為Potatoswatter給出的原因。 除了要求復制/移動構造函數按值返回之外,您根本不能按值返回某些類型:

#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, "");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM