如何有意导致模板实例化的编译时错误 [英] How to intentionally cause a compile-time error on template instantiation

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

问题描述

有时,当使用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.

推荐答案

你可以省略定义它。

You could just omit defining it.

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*>;

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆