为什么析构函数不能成为模板?

Ori*_*ent 2 c++ templates destructor sfinae

我不想在类似联合的类中使用SFINAE来禁用用户声明的析构函数,就像通常用于类中的构造函数一样:

#include <type_traits>
#include <cstdlib>

template< typename A, typename B >
struct U
{
    constexpr U() = default;
    cosntexpr U(A _a) : a(_a), s{false} { ; }
    constexpr U(B _b) : b(_b), s{true}  { ; }

    union
    {

        A a;
        B b;

    };

    bool s;

    template< typename = std::enable_if_t< !(std::is_trivially_destructible< A >{} && std::is_trivially_destructible< B >{}) >, // disable if A and B is trivially destructible
              bool is_noexcept = (noexcept(std::declval< A >().~A()) && noexcept(std::declval< B >().~B())) > // disable if A or B is not destructible
    ~U() noexcept(is_noexcept)
    {
        if (s) {
            b.~B();
        } else {
            a.~A();
        }
    }  
};

int main()
{ 
    struct A {};
    struct B {};
    U< A, B > u;
    static_assert(std::is_literal_type< A >{});
    static_assert(std::is_literal_type< B >{}); // =>
    static_assert(std::is_literal_type< U< A, B > >{});
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

但得到一个错误:

main.cpp:24:5: error: destructor cannot be declared as a template
    ~U() noexcept(is_noexcept)
    ^
1 error generated.
Failure!
Run Code Online (Sandbox Code Playgroud)

在C++中有这种限制的理论原因吗?或者它只是一个"遗产"?

Pet*_*ter 9

任何类U都可以有一个且只有一个析构函数,它在该类中使用名称声明,~U()并且完全不接受任何参数.

模板函数指定一系列函数,其中"族"描述包含一个或多个的集合,而成员数量没有上限.

这两个概念是相互排斥的.一个类不能同时具有一个析构函数的析构函数.