Why does initializing my object with variable template arguments require a move constructor definition?

I want to create a local object of some typename Typein a template function:

template <typename Type, typename... Args>
void create_local(Args... args)
{
    Type val(args...);
}

Now when I call this function with no arguments (where Typeis the class with the non-copyable element):

struct T {
    std::mutex m;
};

int main()
{
    T t;               // OK: No use of move constructor
    create_local<T>(); // Error: Requires deleted move constructor!!!
}

(coliru link)

g ++ (4.7.3 to 5.2) fails to compile and requires the definition of a move constructor T? clang 3.7 compiles successfully.

Also, if I (1) remove a member std::mutexfrom T, (2) declares a default constructor for T, and (3) declares a remote copy instance for T:

struct T {
    T() = default;
    T(const T&) = delete;
};

int main()
{
    T t;               // OK: No use of move constructor
    create_local<T>(); // OK: No use of move constructor
}

all versions of g ++ and clang successfully compiled. Why is g ++ not compiling for any type Typewith non-copied members?

+4
1

:

, 59141 - .

, t ({}), t().

14.5.3p6 , , obj ( ), clang .

+1

Source: https://habr.com/ru/post/1613449/


All Articles