gpt4 book ai didi

c++ - 是否可以编写不接受任何类型的 C++ 模板?

转载 作者:行者123 更新时间:2023-11-28 00:11:43 25 4
gpt4 key购买 nike

我很好奇是否有可能编写这样的 C++ 模板,它不可能创建一个它会接受的类/类型(无错误编译)。如果可能,它可能是什么?

最佳答案

当然。例如:

template <typename T>
struct S {
static_assert(std::is_class<T>::value, "T must be a class");
static_assert(!std::is_class<T>::value, "T must not be a class");
};

但是,这样的模板自动是格式错误的 NDR。

If no valid specialization can be generated for a template, and that template is not instantiated, the template is ill-formed, no diagnostic required.

([温度.res]/8)

关于c++ - 是否可以编写不接受任何类型的 C++ 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32729353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com