Jordan M
Jordan M

Reputation: 1243

Why does std::is_copy_constructible<T&&>::value return false?

It seems that is_copy_constructible<T&&> is false even when is_copy_constructible<T> is true for identical types T. I've tested this with gcc and with clang and get the same results. Is this expected behaviour? Where does the standard define this? What is the reasoning for this behaviour?

Sample code which compiles with the error "int&& doesn't work":

#include <type_traits>

int main() {
    static_assert(std::is_copy_constructible<int>::value, "int doesn't work");
    static_assert(std::is_copy_constructible<int&>::value, "int& doesn't work");
    static_assert(std::is_copy_constructible<int&&>::value, "int&& doesn't work");

    return 0;
}

I encountered this situation when I was trying to create a constraint on a template parameter to ensure that the template only works on copy constructible types. Will I need to create a special case for r-value reference types in order to get my template to work with r-value references to copy constructible types?

Upvotes: 2

Views: 439

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477378

is_copy_constructible<T> is defined as is_constructible<T, const T&>. For T = int &&, the latter becomes is_constructible<int&&, int&>, which is clearly false:

int & f();
int && r(f());   // ill-formed

Upvotes: 4

Related Questions