std::is_constructible 没有给出正确的结果
std::is_constructible doesn't give the correct result
源自 this 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
描述中,有这个 innocent-looking 引用:
Only the validity of the immediate context of the [imaginary v
] variable initialization is considered.
然后注释解释其含义:
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.
我的解释是当你写:
aggregate_wrapper<foo_t> v;
您正在使用 aggregate_wrapper
的默认构造函数,它存在并且可以访问,因此它成功了,至少在 直接上下文 中是这样。然后,non-immediate 上下文包含构造函数的主体,这失败了,但这不会改变 is_constructible
.
的结果
源自 this 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
描述中,有这个 innocent-looking 引用:
Only the validity of the immediate context of the [imaginary
v
] variable initialization is considered.
然后注释解释其含义:
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.
我的解释是当你写:
aggregate_wrapper<foo_t> v;
您正在使用 aggregate_wrapper
的默认构造函数,它存在并且可以访问,因此它成功了,至少在 直接上下文 中是这样。然后,non-immediate 上下文包含构造函数的主体,这失败了,但这不会改变 is_constructible
.