std::is_constructible 没有给出正确的结果
std::is_constructible doesn't give the correct result
源自以下 CodeReview 主题:
#include <cstddef>
#include <algorithm>
#include <iostream>
#include <type_traits>
#include <utility>
template <typename T>
class aggregate_wrapper : public T {
private:
using base = T;
public:
using aggregate_type = T;
template <typename... Ts>
aggregate_wrapper(Ts&&... xs)
: base{std::forward<Ts>(xs)...} {
// nop
}
};
struct foo_t {
foo_t(int) {}
};
int main() {
std::cout << std::is_constructible<foo_t>::value << std::endl;
std::cout << std::is_constructible<aggregate_wrapper<foo_t>>::value << std::endl;
// aggregate_wrapper<foo_t> v; // won't compile
}
当aggregate_wrapper<foo_t> v;
实际上没有编译时,std::is_constructible<aggregate_wrapper<foo_t>>::value
怎么可能是真的?
在C++标准中,在is_constructible
描述中,有这样一句无辜的话:
只考虑 [虚
v
] 变量初始化的直接上下文的有效性。
然后是一个注释,解释它的含义:
初始化的评估可以导致 诸如类模板专用化和函数模板专用化的实例化等效果, 生成隐式定义的函数,依此类推。这种副作用不在直接上下文中,可能导致程序格式不正确。
我的解释是,当你写:
aggregate_wrapper<foo_t> v;
您正在使用默认构造函数 aggregate_wrapper
,它存在并且可访问,因此它成功了,至少在直接上下文中是这样。然后,非即时上下文包括构造函数的主体,并且失败,但这不会改变is_constructible
的结果。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 算术运算的结果类似于:C浮点变量中的1/3
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 如果一个字符,比如说"9",我减去"0",我在结果上使用 is 数字,我是真还是假