如何在模板实例化时故意导致编译时错误

How to intentionally cause a compile-time error on template instantiation(如何在模板实例化时故意导致编译时错误)

本文介绍了如何在模板实例化时故意导致编译时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时在使用 C++ 模板进行编码时,您希望防止用户实例化特定的专业化或专业化集,因为结果将是无意义的.因此,您可以定义(特定的或部分的)特化,其定义如果被实例化,将导致编译器错误.目标是,如果用户误用"模板,在头文件中解释不该做什么的注释旁边导致编译器错误,而不是让编译器自己提出一些令人困惑的错误消息设备,或者可能允许编译有问题的代码.

Sometimes when coding with C++ templates, you want to prevent users from instantiating a specific specialization or set of specializations, because the result would be nonsensical. So you can define a (specific or partial) specialization whose definition, if instantiated, would cause a compiler error. The goal would be, if a user "misuses" the template, to cause a compiler error right next to a comment in your header file explaining what not to do, rather than leaving the compiler to come up with some confusing error message by its own devices, or maybe allowing the questionable code to compile.

示例:

template <typename T> struct MyClassTemplate {
  // ...
};

template <typename T> struct MyClassTemplate<T*> {
  // Do not use MyClassTemplate with a pointer type!
  typedef typename T::intentional_error err;
};

有多种方法可以做到这一点(取决于您的专业化是类或函数的完全专业化还是部分专业化).但是使用的语法必须 (?) 依赖于模板参数,否则编译器在第一次解析故意错误定义时会报错.上面的例子有一个漏洞,有人可以顽固地定义一个 intentional_error 嵌套类型或成员 typedef(尽管我会说他们应该因此而出现任何问题).但是,如果您使用太花哨的技巧,您很可能会收到无法辨认和/或误导性的编译器错误消息,这在很大程度上与目的背道而驰.

There are a number of ways to do this (depending on whether your specialization is a complete or partial specialization of a class or function). But the syntax used must (?) depend on a template parameter, or else the compiler will complain when it first parses the intentional-error definition. The example above has a hole in that somebody could stubbornly define an intentional_error nested type or member typedef (though I'd say they would then deserve whatever problems come up as a result). But if you use a trick too fancy, you're likely to get an indecipherable and/or misleading compiler error message, which mostly defeats the purpose.

是否有更直接的方法来禁止模板实例化?

Are there better straightforward ways to disallow template instantiations?

我知道在 C++0x 中,模板概念和删除的函数声明可以更好地控制此类事情,但我正在寻找有效的 C++03 答案.

I'm aware that in C++0x, template Concepts and deleted function declarations will provide much better control over this sort of thing, but I'm looking for answers that are valid C++03.

推荐答案

你可以省略定义它.

template <typename T> struct MyClassTemplate<T*>;

您也可以从未定义的专业化派生

You could also derive from a non-defined specialization

template <typename T> struct invalid;
template <typename T> struct MyClassTemplate<T*> : invalid<T> { };

请注意,声明类或函数的显式特化永远不会依赖于模板参数.所以,像这样依赖模板参数的东西无论如何都行不通.在这种情况下,声明一个未定义的显式特化就足够了

Note that explicit specializations that declare classes or functions will never depend on template parameters. So, stuff like this that depend on template parameters can't work anyway. In that case, declaring a non-defined explicit specialization should be sufficient

template<> struct MyClassTemplate<int*>;

这篇关于如何在模板实例化时故意导致编译时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在模板实例化时故意导致编译时错误

基础教程推荐