- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
使用 g++-5 我得到以下输出
#include <type_traits>
#include <tuple>
int main()
{
bool b;
b = std::is_default_constructible<int>::value; //Compiles, returns true
b = std::is_default_constructible<int&>::value; //Compiles, returns false
b = std::is_default_constructible< std::tuple<int> >::value; //Compiles, returns true
b = std::is_default_constructible< std::tuple<int&> >::value; //Does not compile
}
这是 is_default_constructible
实现中的错误吗?
错误信息是一个很长的堆栈列表,结尾是:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.1.0/../../../../include/c++/5.1.0/tuple:105:9: error: reference to type 'int' requires an initializer
: _M_head_impl() { }
最佳答案
这不是 is_default_constructible
中的错误.该类型特征只需要检查默认构造的直接上下文,它不必深入评估任何成员初始值设定项。这个限制可能是为了可以通过使用 SFINAE 在没有专用编译器魔法的情况下实现它。 (参见 [meta.unary.prop],尤其是第 7 页)。
tuple
和 pair
如果元素类型不能默认构造,则默认构造函数不需要在直接上下文中失败(SFINAE 友好)。 LWG 2367 已解决此问题,它为 tuple
引入了以下 SFINAE 要求默认构造函数:
Remarks: This constructor shall not participate in overload resolution unless
is_default_constructible<Ti>::value
is true for alli
. [...]
有了这个附加要求,元组的默认构造必须以 SFINAE 友好的方式失败,这样 is_default_constructible
现在适用于 tuple
如果元素无法在直接上下文中默认构造(引用类型就是这种情况)。
LWG 2367 目前处于就绪状态;提议的决议尚未(尚未)纳入 github 草案。
[-- 这部分还在考虑中
Yakk在评论中提出了一个重要的观点:为什么 is_default_constructible
必须深度实例化成员初始值设定项?
据我所知,这与条件 constexpr
有关tuple
的活跃度的默认构造函数。 is_default_constructible
导致默认构造函数的实例化。它只需要实例化声明,以确定是否可以在即时上下文 中无故障地调用此构造函数。但是,声明的实例化需要确定 constexpr
'iveness,这导致构造函数定义的实例化。
标记为constexpr
的类模板的成员函数(或构造函数)只是有条件的 constexpr
: 只有那些类模板实例化的成员函数才会是 constexpr
body 不违反constexpr
的地方限制。这需要构造函数主体的实例化,以便构造函数检查成员初始值设定项是否允许在 constexpr
中使用。功能。考虑:
struct nonconstexpr { nonconstexpr() { std::cout << "runtime\n"; } };
struct isconstexpr { constexpr isconstexpr() {} };
template<typename T>
struct wrapper { T t; constexpr wrapper() : t() {} };
在实例化 wrapper
的默认构造函数时, 编译器必须实例化成员初始值设定项以确定此实例化是否应为 constexpr
.
在std::tuple
的情况下,这会在某处导致成员初始化器的实例化,该成员初始化器试图对引用元组叶(数据成员)进行值初始化。这是一个错误,它不会在默认构造函数的原始实例化的直接上下文中发生。因此,这是一个硬错误,而不是直接上下文中的替换失败。
--]
这部分我不是很清楚因为CWG 1358基本上使所有实例化constexpr
, 他们是否真的满足标准。事实上,gcc 6.0 不会编译以下示例,而 gcc 5.1 和 clang 3.7 会拒绝它:
#include <type_traits>
template<typename T>
struct foo
{
T t;
constexpr foo() {} // remove `constexpr` to make it compile everywhere
};
int main()
{
static_assert(std::is_default_constructible<foo<int&>>{}, "!");
}
CWG 1358 还告诉我们为什么这两种方法(条件 constexpr 和 constexpr 尽管违反)之间的区别很重要:
Questions arose in the discussion of issue 1581 as to whether this approach — making the specialization of a constexpr function template or member function of a class template still constexpr but unable to be invoked in a constant context — is correct. The implication is that class types might be categorized as literal but not be able to be instantiated at compile time. This issue is therefore returned to "review" status to allow further consideration of this question.
对于 libc++,存在错误 #21157 ,已于 2014-10-15 解决并出现在 clang3.6 分支中。对于 libstdc++,似乎没有错误报告;该问题已在 combined commit on 2015-06-30 中修复它还实现了 N4387 - Improving Pair and Tuple (Revision 3)目前似乎没有出现在任何 gcc5 分支中。
关于c++ - 检查引用元组是否默认可构造时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433911/
我有一个 if 语句,如下所示 if (not(fullpath.lower().endswith(".pdf")) or not (fullpath.lower().endswith(tup
然而,在 PHP 中,可以: only appears if $foo is true. only appears if $foo is false. 在 Javascript 中,能否在一个脚
XML有很多好处。它既是机器可读的,也是人类可读的,它具有标准化的格式,并且用途广泛。 它也有一些缺点。它是冗长的,不是传输大量数据的非常有效的方法。 XML最有用的方面之一是模式语言。使用模式,您可
由于长期使用 SQL2000,我并没有真正深入了解公用表表达式。 我给出的答案here (#4025380)和 here (#4018793)违背了潮流,因为他们没有使用 CTE。 我很欣赏它们对于递
我有一个应用程序: void deleteObj(id){ MyObj obj = getObjById(id); if (obj == null) { throw n
我的代码如下。可能我以类似的方式多次使用它,即简单地说,我正在以这种方式管理 session 和事务: List users= null; try{ sess
在开发J2EE Web应用程序时,我通常会按以下方式组织我的包结构 com.jameselsey.. 控制器-控制器/操作转到此处 服务-事务服务类,由控制器调用 域-应用程序使用的我的域类/对象 D
这更多是出于好奇而不是任何重要问题,但我只是想知道 memmove 中的以下片段文档: Copying takes place as if an intermediate buffer were us
路径压缩涉及将根指定为路径上每个节点的新父节点——这可能会降低根的等级,并可能降低路径上所有节点的等级。有办法解决这个问题吗?有必要处理这个吗?或者,也许可以将等级视为树高的上限而不是确切的高度? 谢
我有两个类,A 和 B。A 是 B 的父类,我有一个函数接收指向 A 类型类的指针,检查它是否也是 B 类型,如果是将调用另一个函数,该函数接受一个指向类型 B 的类的指针。当函数调用另一个函数时,我
有没有办法让 valgrind 使用多个处理器? 我正在使用 valgrind 的 callgrind 进行一些瓶颈分析,并注意到我的应用程序中的资源使用行为与在 valgrind/callgrind
假设我们要使用 ReaderT [(a,b)]超过 Maybe monad,然后我们想在列表中进行查找。 现在,一个简单且不常见的方法是: 第一种可能性 find a = ReaderT (looku
我的代码似乎有问题。我需要说的是: if ( $('html').attr('lang').val() == 'fr-FR' ) { // do this } else { // do
根据this文章(2018 年 4 月)AKS 在可用性集中运行时能够跨故障域智能放置 Pod,但尚不考虑更新域。很快就会使用更新域将 Pod 放入 AKS 中吗? 最佳答案 当您设置集群时,它已经自
course | section | type comart2 : bsit201 : lec comart2 :
我正在开发自己的 SDK,而这又依赖于某些第 3 方 SDK。例如 - OkHttp。 我应该将 OkHttp 添加到我的 build.gradle 中,还是让我的 SDK 用户包含它?在这种情况下,
随着 Rust 越来越充实,我对它的兴趣开始激起。我喜欢它支持代数数据类型,尤其是那些匹配的事实,但是对其他功能习语有什么想法吗? 例如标准库中是否有标准过滤器/映射/归约函数的集合,更重要的是,您能
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我一直在研究 PHP 中的对象。我见过的所有示例甚至在它们自己的对象上都使用了对象构造函数。 PHP 会强制您这样做吗?如果是,为什么? 例如: firstname = $firstname;
...比关联数组? 关联数组会占用更多内存吗? $arr = array(1, 1, 1); $arr[10] = 1; $arr[] = 1; // <- index is 11; does the
我是一名优秀的程序员,十分优秀!