这是一个让我感到困惑的小问题。我不知道如何描述它,所以只看下面的代码:
struct B {
B() {}
B(B&) {
std::cout << "not trivial\n";
}
};
int main() {
B b1;
B b2(b1);
std::cout << std::is_trivially_constructible<B, B&>::value << '\n';
return 0;
}
输出是:
not trivial
1
我正在使用VS11。
编辑:
我刚刚测试了http://en.cppreference.com/w/cpp/types/is_constructible中的示例。某些输出不正确。
#include <iostream>
#include <type_traits>
class Foo {
int v1;
double v2;
public:
Foo(int n) : v1(n), v2() {}
Foo(int n, double f) : v1(n), v2(f) {}
};
int main() {
std::cout << "Foo is ...\n" << std::boolalpha
<< "\tTrivially-constructible from const Foo&? "
<< std::is_trivially_constructible<Foo, const Foo&>::value << '\n'
<< "\tTrivially-constructible from int? "
<< std::is_trivially_constructible<Foo, int>::value << '\n'
<< "\tConstructible from int? "
<< std::is_constructible<Foo, int>::value << '\n'
}
输出是:
Foo is ...
Trivially-constructible from const Foo&? true
Trivially-constructible from int? true//Trivially-constructible from int? false
Constructible from int? true