- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
用std::declval
不是更好吗?声明形式:
template< class T > T declval(); // (1)
然后是当前的:
template< class T > T && declval(); // (2)
std::common_type
(可能仅出于当前目的使用不同的名称)?
common_type
的行为使用 (1)
比使用 std::decay_t
时的行为更接近三元运算符(但未使用 (2)
)的行为:
template< typename T >
T declval();
template <class ...T> struct common_type;
template< class... T >
using common_type_t = typename common_type<T...>::type;
template <class T>
struct common_type<T> {
typedef T type;
};
template <class T, class U>
struct common_type<T, U> {
typedef decltype(true ? declval<T>() : declval<U>()) type;
};
template <class T, class U, class... V>
struct common_type<T, U, V...> {
typedef common_type_t<common_type_t<T, U>, V...> type;
};
#include <type_traits>
#include <utility>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunevaluated-expression"
int
main()
{
int i{};
static_assert(std::is_same< int &, decltype((i)) >{});
static_assert(std::is_same< int , std::common_type_t< decltype((i)), decltype((i)) > >{});
static_assert(std::is_same< int &, decltype(true ? i : i) >{});
static_assert(std::is_same< int &, common_type_t< decltype((i)), decltype((i)) > >{});
int && k{};
static_assert(std::is_same< int &&, decltype(k) >{});
static_assert(std::is_same< int , std::common_type_t< decltype(k), decltype(k) > >{});
static_assert(std::is_same< int &&, decltype(true ? std::move(k) : std::move(k)) >{});
static_assert(std::is_same< int &&, common_type_t< decltype(k), decltype(k) > >{});
return 0;
}
#pragma clang diagnostic pop
这种方法有什么缺点?是真的吗,(1)
在 decltype()
上下文类型 T
应该是可构造的(完全是,即应该至少有一个构造函数)和/或可破坏的?
For non-specialized std::common_type, the rules for determining the common type between every pair T1, T2 are exactly the rules for determining the return type of the ternary conditional operator in unevaluated context, with arbitrary first argument of type bool and with
xvalues of type T1 and T2 (since C++17)
std::declval<T1>() and std::declval<T2>() (until C++17)
as the second and the third operands.The common type is the result of std::decay applied to the type of the ternary conditional (since C++14).
我认为最后一句 ( emphasized
) 很可能不应该只是 since C++14
还有until C++17
公平起见。否则,即使在 C++17 之后,引用的第一句也不会成立,并且会出现一些缺陷。
should-stdcommon-type-use-stddecay 中有一些说明关于 std::common_type
的评论问题,但这只是当前问题的背景信息。
最佳答案
优势:
template <class T> T&& declval();
它是否适用于任何类型 T
,而只是返回 T
不适用于不可返回的类型(例如函数、数组)和不可破坏的类型(例如私有(private)/ protected /删除的析构函数、抽象基类)。
当然,缺点是common_type<int, int>
最终成为 int&&
, 然后你需要添加 decay
这使得 common_type<int&, int&>
是int
- 这也没有意义。这里没有胜利。
最终,我认为我们只需要一些语言功能,在未评估的上下文中,“给我一些 T
类型的东西”适用于任何 T
,那真的给了你一个T
(而不是 T&&
)。
关于c++ - T declval() 而不是 T && declval() for common_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35525912/
我试图了解 declval() 之间的区别和 declval() ?有没有示例 T&可以在 T 时使用不能吗? #include #include struct X { X() = delet
用std::declval不是更好吗?声明形式: template T declval(); // (1) 然后是当前的: template T && declval(); // (2) std::c
我想弄清楚如何在 is_assignable 的实现中解释 declval() = declval()。 declval 将类型转换为引用。鉴于此,我将表达式转换为以下四种可能性之一: _Dest&&
我正在尝试使用 detector我是根据std::experiment::is_detected_v写的检查类型是否可分配。然而std::declval() = std::declval()无效且只有
根据 is_destructible 的定义( http://eel.is/c++draft/meta.unary.prop#lib:is_destructible ), is_destructib
标准库实用程序 declval是defined作为: template add_rvalue_reference_t declval() noexcept; 在 C++11 中引入右值引用似乎是个好主
我想尝试编写一个模板包装器来检查类是否具有成员函数。为此,有必要使用 std::declval template struct has_member().push_back())>>:std::tru
以下代码在模板参数T时编译失败是基本类型,例如 int (在 gcc 4.8 上)。这是符合标准的行为吗?我对std::declval的理解是它总是解析为 T&&或 T& . template vo
引用以下Proposing Standard Library Support for the C++ Detection Idiom中的例子: // primary template handles
假设我有一个将要专门化的模板化函数,所以我真的不关心基本实现。我可以做这样的事情吗: template T dummy() { assert(false); return declv
一个最小的例子,展示了两种获取迭代器类型的方法,我天真地希望得到与结果相同的类型: template struct foo { using iterator = decltype(std::
有人可以解释一下 std::declval 是如何工作的吗?我在 gcc headers/type_traits(第 2248-2262 行)中找到了这个实现,它是(清理了一下以提高可读性): tem
我在玩 GCC (4.9.2) abi::__cxa_demangle 时遇到了无法分解特定符号名称的情况。 那个符号是: _ZNK12DebugWrapperIR5TestClsE5getIdIIE
当您使用模板和 decltype 时你经常需要某种类型的实例,即使你没有任何时间。在这种情况下,std::declval()非常有用。这将创建一个 T 类型的虚构实例. 有没有类似的概念?即一个函数,
查看libstdc++源码,发现如下declval实现: template _Up __declval(int); // (1) template _Tp __declval(long); // (
我正在编写一些元函数,并且我有想法使用 C++17 的 if constexpr 结合推导的返回类型来编写它们。这是一个例子: #include #include #include templa
cppreference web site引入 declval : Note that because no definition exists for declval, it can only be
std::declval是一个编译时实用程序,用于构造表达式以确定其类型。它是这样定义的: template typename std::add_rvalue_reference::type decl
据我所知,我无法声明对 void 的右值引用. 例如,以下代码格式错误: void f(void &&v) { } 来自 [20.2.6/1] (函数模板 declval)我们有一个 declval
std::declval是一个编译时实用程序,用于构造表达式以确定其类型。它是这样定义的: template typename std::add_rvalue_reference::type decl
我是一名优秀的程序员,十分优秀!