简体   繁体   中英

decltype with deleted constructor is possible in template function

As I know decltype is not allowed to use deleted constructor:

struct no_def
{
    no_def() = delete;
};

void test()
{
    decltype(no_def()) a{}; //error: use of deleted function ‘no_def::no_def()’
}

but if I make template "test" function it will compile

template<typename...>
void test()
{
    decltype(no_def()) a{}; //OK
}

and it also

template<typename...>
void test()
{
    decltype(no_def("not", "defined", "constructor")) a{}; //OK
}

could someone explain it?

It's apparently a bug in GCC. Both the latest Clang and the latest Visual C++ correctly print a diagnostic message.

Clang:

error: call to deleted constructor of 'no_def'

Visual C++:

error C2280: 'no_def::no_def(void)': attempting to reference a deleted function

You can test this for yourself at https://godbolt.org/ .


Note that in order to verify the bug, you should simplify the template, call the function, and get rid of unused-variable warnings which interfere with the output you are interested in:

struct no_def
{
    no_def() = delete;
};

template<typename T>
void test()
{
    decltype(no_def()) a{}; // error in Clang and MSVC, no error in GCC
    a = a; // get rid of warning
}

int main()
{
    test<int>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM