1. <tfoot id='B5Pgy'></tfoot>
    2. <i id='B5Pgy'><tr id='B5Pgy'><dt id='B5Pgy'><q id='B5Pgy'><span id='B5Pgy'><b id='B5Pgy'><form id='B5Pgy'><ins id='B5Pgy'></ins><ul id='B5Pgy'></ul><sub id='B5Pgy'></sub></form><legend id='B5Pgy'></legend><bdo id='B5Pgy'><pre id='B5Pgy'><center id='B5Pgy'></center></pre></bdo></b><th id='B5Pgy'></th></span></q></dt></tr></i><div id='B5Pgy'><tfoot id='B5Pgy'></tfoot><dl id='B5Pgy'><fieldset id='B5Pgy'></fieldset></dl></div>
    3. <small id='B5Pgy'></small><noframes id='B5Pgy'>

      • <bdo id='B5Pgy'></bdo><ul id='B5Pgy'></ul>
      <legend id='B5Pgy'><style id='B5Pgy'><dir id='B5Pgy'><q id='B5Pgy'></q></dir></style></legend>

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

      时间:2023-06-04

      • <small id='aTyzB'></small><noframes id='aTyzB'>

        <legend id='aTyzB'><style id='aTyzB'><dir id='aTyzB'><q id='aTyzB'></q></dir></style></legend>

          • <tfoot id='aTyzB'></tfoot>
            1. <i id='aTyzB'><tr id='aTyzB'><dt id='aTyzB'><q id='aTyzB'><span id='aTyzB'><b id='aTyzB'><form id='aTyzB'><ins id='aTyzB'></ins><ul id='aTyzB'></ul><sub id='aTyzB'></sub></form><legend id='aTyzB'></legend><bdo id='aTyzB'><pre id='aTyzB'><center id='aTyzB'></center></pre></bdo></b><th id='aTyzB'></th></span></q></dt></tr></i><div id='aTyzB'><tfoot id='aTyzB'></tfoot><dl id='aTyzB'><fieldset id='aTyzB'></fieldset></dl></div>
                <tbody id='aTyzB'></tbody>
              • <bdo id='aTyzB'></bdo><ul id='aTyzB'></ul>
                本文介绍了如何在模板实例化时故意导致编译时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有时在使用 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*>;
                

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

                上一篇:C++函数中局部类的使用 下一篇:stdafx.h 的用途

                相关文章

                  <legend id='220oH'><style id='220oH'><dir id='220oH'><q id='220oH'></q></dir></style></legend>

                    <bdo id='220oH'></bdo><ul id='220oH'></ul>
                1. <tfoot id='220oH'></tfoot>

                  <small id='220oH'></small><noframes id='220oH'>

                2. <i id='220oH'><tr id='220oH'><dt id='220oH'><q id='220oH'><span id='220oH'><b id='220oH'><form id='220oH'><ins id='220oH'></ins><ul id='220oH'></ul><sub id='220oH'></sub></form><legend id='220oH'></legend><bdo id='220oH'><pre id='220oH'><center id='220oH'></center></pre></bdo></b><th id='220oH'></th></span></q></dt></tr></i><div id='220oH'><tfoot id='220oH'></tfoot><dl id='220oH'><fieldset id='220oH'></fieldset></dl></div>