c – 当类被实例化时,类模板的成员是否被实例化?

假设模板类的成员不应该被实例化,除非它们被使用.
但是,这个示例似乎实例化了do_something成员,并且enable_if失败了(如果我们实例化了 – 但是AFAIK,我们没有这样做).

我错过了一些非常基本的东西吗?

#include <string>
#include <boost/utility.hpp>

struct some_policy {
    typedef boost::integral_constant<bool,false> condition;
};

struct other_policy {
    typedef boost::integral_constant<bool,true> condition;
};


template <typename policy>
class test {
   void do_something(typename boost::enable_if<typename policy::condition>::type* = 0) {}
};

int main() {
    test<other_policy> p1;
    test<some_policy>  p2;
}

coliru

解决方法

从C 11 14.7.1 / 1:

The implicit instantiation of a class template specialization causes the implicit
instantiation of the declarations,but not of the deFinitions or default arguments,of the class member functions

所以函数声明被实例化了;它失败,因为它取决于无效类型.

(不幸的是,我没有任何历史版本的标准,但我想象这个规则在C98中是相似的)

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...