如果实例化,如何使模板化变量专用化在编译时失败

How can I make a templated variable specialization fail at compile time if instantiated?

本文关键字:专用 编译 失败 变量 如果 何使模 实例化      更新时间:2023-10-16

当且仅当模板化变量的默认专用化被实例化时,是否有可能出现编译时错误?例如

template<typename T>
constexpr int foo = /* Something that fails */;
template<>
constexpr int foo<bool> = 42;
// ...
int bar = foo<bool>; // All good!
int meow = foo<int>; // Error if and only if this line exists

我尝试输入的所有/* Something that fails*/最终都失败了,即使没有实例化专业化。这可能吗?如果可以通过像static_assert这样的机制以某种方式报告错误,以便至少在某种程度上清晰易读,那就更好了。

如果这是标准的,您应该询问语言律师。Clang 不会让你将模板化的 constexpr 变量保留为未定义,但它会让你从 constexpr 初始值设定项引用未定义的模板实例化。然后你可以这样写:

template<typename T>
struct no_such_type_for_foo;
template<typename T>
constexpr int foo = no_such_type_for_foo<T>::value;
template<>
constexpr int foo<int> = 4;
int main()
{
    int y = foo<int>; // all good
    int z = foo<bool>; // implicit instantiation of undefined template 'no_such_type_for_foo<bool>'
}

gcc不喜欢模板实例化中的static关键字。

但是,仅未定义默认模板似乎就可以解决问题:

template<typename T>
constexpr int foo;
template<>
constexpr int foo<bool> = 42;

有了这个,这就可以了:

std::cout << foo<bool> << std::endl;

这失败了:

std::cout << foo<char> << std::endl;

跟:

t.C:2:15: error: uninitialized const ‘foo<char>’ [-fpermissive]
 constexpr int foo;
               ^

我认为这种情况与未定义默认模板的更常见情况之间没有太大区别:

template<typename T> class foo;
template<>
class foo<char> {
// ...
};

同样的事情。

基于 zneak 和 Sam 的解决方案,我想出了一个允许通过 static_assert 自定义错误消息的变体。关键是static_assert条件需要依赖于模板参数,否则无论是否实际使用模板,都会立即进行评估。

问题是我们希望static_assert无条件失败,因此对于每个可能的参数,条件应该减少到false。我们依赖于编译器本身不进行分析(我不确定如果模板没有实例化,它是否真的会被允许弄清楚)。

template<typename T>
constexpr int no_such_type_for_foo()
{
    static_assert(sizeof(T) < 0, "No such type for foo");
    return 0;
}
template<typename T>
constexpr int foo = no_such_type_for_foo<T>();
template<>
constexpr int foo<bool> = 42;
int main()
{
    int y = foo<bool>; // all good
    int z = foo<int>; // static_assert failed "No such type for foo"
}