<bdo id='nU8ir'></bdo><ul id='nU8ir'></ul>

      <small id='nU8ir'></small><noframes id='nU8ir'>

    1. <i id='nU8ir'><tr id='nU8ir'><dt id='nU8ir'><q id='nU8ir'><span id='nU8ir'><b id='nU8ir'><form id='nU8ir'><ins id='nU8ir'></ins><ul id='nU8ir'></ul><sub id='nU8ir'></sub></form><legend id='nU8ir'></legend><bdo id='nU8ir'><pre id='nU8ir'><center id='nU8ir'></center></pre></bdo></b><th id='nU8ir'></th></span></q></dt></tr></i><div id='nU8ir'><tfoot id='nU8ir'></tfoot><dl id='nU8ir'><fieldset id='nU8ir'></fieldset></dl></div>
      <legend id='nU8ir'><style id='nU8ir'><dir id='nU8ir'><q id='nU8ir'></q></dir></style></legend>
      <tfoot id='nU8ir'></tfoot>
      1. 如何防止非专业模板实例化?

        时间:2023-05-24

            <tfoot id='eCa3Y'></tfoot>

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

              <small id='eCa3Y'></small><noframes id='eCa3Y'>

            • <i id='eCa3Y'><tr id='eCa3Y'><dt id='eCa3Y'><q id='eCa3Y'><span id='eCa3Y'><b id='eCa3Y'><form id='eCa3Y'><ins id='eCa3Y'></ins><ul id='eCa3Y'></ul><sub id='eCa3Y'></sub></form><legend id='eCa3Y'></legend><bdo id='eCa3Y'><pre id='eCa3Y'><center id='eCa3Y'></center></pre></bdo></b><th id='eCa3Y'></th></span></q></dt></tr></i><div id='eCa3Y'><tfoot id='eCa3Y'></tfoot><dl id='eCa3Y'><fieldset id='eCa3Y'></fieldset></dl></div>
                <tbody id='eCa3Y'></tbody>

              • <bdo id='eCa3Y'></bdo><ul id='eCa3Y'></ul>
                1. 本文介绍了如何防止非专业模板实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个模板化的class(称之为Foo),它有几个专门化.如果有人尝试使用 Foo 的非专业版本,我希望编译失败.

                  I have a templated class (call it Foo) which has several specializations. I would like the compilation to fail if someone tries to use an unspecialized version of Foo.

                  这是我实际拥有的:

                  template <typename Type>
                  class Foo
                  {
                    Foo() { cannot_instantiate_an_unspecialized_Foo(); }
                  
                    // This method is NEVER defined to prevent linking.
                    // Its name was chosen to provide a clear explanation why the compilation failed.
                    void cannot_instantiate_an_unspecialized_Foo();
                  };
                  
                  template <>
                  class Foo<int>
                  {    };
                  
                  template <>
                  class Foo<double>
                  {    };
                  

                  所以:

                  int main()
                  {
                    Foo<int> foo;
                  }
                  

                  同时工作:

                  int main()
                  {
                    Foo<char> foo;
                  }
                  

                  没有.

                  显然,编译器链只会在链接过程发生时发出警告.但是有没有办法让它在之前抱怨?

                  Obviously, the compiler chain only complains when the linking process takes place. But is there a way to make it complain before ?

                  我可以使用boost.

                  推荐答案

                  不要定义类:

                  template <typename Type>
                  class Foo;
                  
                  template <>
                  class Foo<int> { };
                  
                  int main(int argc, char *argv[]) 
                  {
                      Foo<int> f; // Fine, Foo<int> exists
                      Foo<char> fc; // Error, incomplete type
                      return 0;
                  }
                  

                  为什么会这样?仅仅是因为没有任何通用模板.声明,是,但未定义.

                  Why does this work? Simply because there isn't any generic template. Declared, yes, but not defined.

                  这篇关于如何防止非专业模板实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:模板:模板函数不能很好地与类的模板成员函数配 下一篇:GCC 错误:非命名空间范围内的显式特化

                  相关文章

                  最新文章

                  1. <legend id='fkGou'><style id='fkGou'><dir id='fkGou'><q id='fkGou'></q></dir></style></legend>

                      <small id='fkGou'></small><noframes id='fkGou'>

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