Reputation: 1921
After some experimenting I've managed to write a piece of code that will check whether the move constructor of a class is noexcept (without using type_traits; this is just an exercise):
std::cout << std::boolalpha << noexcept(Widget(std::declval<Widget>())) << std::endl;
The declval function "returns" Widget&&, so the whole expression is a move constructor call. But how to achieve something similar for the copy constructor?
Upvotes: 3
Views: 1338
Reputation: 96845
Use std::declval<Widget&>()
. Reference collapsing will turn T&& &
into T&
:
std::cout << std::boolalpha << noexcept(Widget(std::declval<Widget&>())) << std::endl;
// ^^^^^^^^
Upvotes: 9
Reputation: 65640
You could declare your own declval
-like function:
template< class T >
typename std::add_lvalue_reference<T>::type mydeclval() noexcept;
//outputs true if the copy constructor is declared noexcept
std::cout << std::boolalpha << noexcept(Widget(mydeclval<Widget>())) << std::endl;
Upvotes: 2