- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
以下程序在使用 GCC 4.7 和 clang 3.2 编译时,会产生“1”作为输出。
#include <type_traits>
struct foo {
template<typename T>
foo(T) {
static_assert(not std::is_same<int, T>(), "no ints please");
}
};
#include <iostream>
int main() {
std::cout << std::is_constructible<foo, int>();
}
这令人困惑。 foo
显然不能从 int
构造!如果我将 main
更改为以下内容,两个编译器都会因为静态断言失败而拒绝它:
int main() {
foo(0);
}
为什么两个编译器都说它是可构造的?
最佳答案
这是标准必须说的(§20.9.5/6),我强调:
Given the following function prototype:
template <class T>
typename add_rvalue_reference<T>::type create();the predicate condition for a template specialization
is_constructible<T, Args...>
shall be satisfied if and only if the following variable definition would be well-formed for some invented variablet
:T t(create<Args>()...);
[ Note: These tokens are never interpreted as a function declaration. —end note ]
Access checking is performed as if in a context unrelated to
T
and any of theArgs
. Only the validity of the immediate context of the variable initialization is considered. [ Note: The evaluation of the initialization can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the “immediate context” and can result in the program being ill-formed. —end note ]
只有在模板构造函数被实例化时,断言才会失败。但是,正如注释中所澄清的那样,该断言不在所考虑的变量定义的直接上下文中,因此不会影响其“有效性”。因此编译器可以将该定义视为有效,从而声称 foo
确实可以从 int
构造,即使实际上试图构造一个 foo
来自 int
导致程序格式错误。
请注意,编译器也可以,而不是 is_constructible
yield false,只是根据断言拒绝原始程序,即使两者都没有。
关于c++ - 为什么 is_constructible 声称某些东西不是可构造的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14815998/
最近我试图检测特定私有(private)构造函数的存在,但遇到了 std::is_constructible 仅检查直接上下文的问题,因此不会识别任何此类构造函数。经过一些研究,我确实看到了一个答案
我想从我可以用模板参数构造的列表中找到类型。在这个例子中: using my_type = typename get_constructible::type; my_type 必须是 std::str
我想检测一个类是否有显式转换运算符。我已尝试使用 is_constructible,但以下断言因 msvc 19.00.23506 而失败。 #include #include struct Fo
这个问题在这里已经有了答案: Why does is_constructible claim something is constructible when it isn't? (2 个答案) 关闭
是否 std::is_constructible工作如果 Arg1是一种可转换为 T 的有效单参数构造函数的类型?当类型具有非模板化转换运算符时它似乎可以工作,但如果转换运算符是模板化的则它不起作用(
以下程序在使用 GCC 4.7 和 clang 3.2 编译时,会产生“1”作为输出。 #include struct foo { template foo(T) {
我有以下代码: #include class A; int main() { std::cout ::value struct is_constructible; T and all ty
源自this话题。也与此有关 topic . 我的问题是为什么 std::is_constructible 在直接上下文中停止?我认为 std::is_constructible 的用户会期望它能够深
我容器的一个构造函数默认构造一个分配器作为默认参数值: template struct my_container { my_container(int n, Allocator alloc = A
我在玩弄模板并尝试实现以下助手。 first_constructible::type 这将返回第一种 Types可从 Args... 构建.第一个问题显然是在 struct 中有两个参数包,所以我将用
std::is_constructible 的规则是什么?处理私有(private)构造函数?给定以下代码: #include class Class { private: Class()
这个问题在这里已经有了答案: C++98/03 std::is_constructible implementation (4 个答案) 关闭 5 年前。 到目前为止,我在网上找不到任何 ELI5。
我的爱好库的基本组件必须与 C++98 和 C++11 编译器一起使用。为了学习和享受自己,我创建了几种类型支持功能的 C++98 实现(如 enable_if、conditional、is_same
std::is_constructible::value 的结果不一致.我对该标准的解释是它应该是错误的。然而,Clang,同时具有 libc++ 和 libstdc++*,给出了 true。 GCC
我在这里冒险,假设这是可能的,但我不太确定。基本上我正在寻找的是一种在编译时在使用默认构造函数或通过引用采用一个参数的构造函数之间切换的方法。 即 T* create() { return n
我目前正在研究一些模板的东西。但我有一个问题。我有一堂这样的课 class myobj{ public: int val; char single; string na
我目前正在研究一些模板的东西。但我有一个问题。我有一堂这样的课 class myobj{ public: int val; char single; string na
在评估 std::is_constructible 和 std::is_destructible 时,Clang 和 GCC 似乎不遵守 friend 声明。 关于`is_constructible,
std::is_constructible 的预期结果是什么?在具有私有(private)或 protected 析构函数的类型上? 例如,即使只有 friend 可以释放它,我仍然可以在堆上构造这样
我正在尝试禁用具有非 std::string 可构造类型的 ctor。我的第一次尝试是这样的: #include struct A { template ::value>::type>
我是一名优秀的程序员,十分优秀!