如何防止C ++模板的专业化?

不是

当我这样做时,编译器不会抱怨;-)

// Myfile.h
#include <iostream>
#include <vector>

namespace std
{

template<> class vector<int>
{
public:
    vector ()
    {
        std::cout << "Happy Halloween !!!\n";
    }
};

}

有什么方法可以防止类/函数模板的这种不良的专业化?

- 编辑 -

我只是以std ::为例。我正在寻找一种防止这种情况发生在任何模板类上的方法。

扬·赫尔曼

一个别名模板不能专业化,有您需要的类模板的行为。

template<class T>
struct my_class_implementation_which_should_not_be_used_directly
{
    // impl
};


template<class T>
using my_class = my_class_implementation_which_should_not_be_used_directly<T>;

另外,您应该记录专门化my_class_implementation_which_should_not_be_used_directly导致未定义行为。现在,您的库用户可以不再专门从事专业工作my_class,而是直接使用丑陋的名称警告该类。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章