简体   繁体   English

C ++:强制完成模板编译(MSVC / G ++)

[英]C++: force complete template compilation (MSVC/G++)

Hello and good day to you. 你好,祝你好运。

Following code fragment compiles on cl.exe (15.00.30729.01) and mingw-g++ (4.4.0): 以下代码片段编译cl.exe(15.00.30729.01)和mingw-g ++(4.4.0):

template<typename T> class Test{
public:
    T t;
    void error(){
        int doesNotExist = 6;
        return doesNotExist;//<---- void function returning result
    }
};

int main(int argc, char** argv){
    Test<int> test;
    return 0;
}

Also, on cl.exe you can even get away with something like this: 此外,在cl.exe上你甚至可以逃避这样的事情:

template<typename T> class Test{
public:
    T t;
    void error(){
        doesNotExist = 6;//<---- undeclared variable
        return doesNotExist;//<---- void function returning result
    }
};

Now, this obviously happens because compiler does not create contents for methods of a template class until somebody calls them. 现在,这显然是因为编译器在有人调用它们之前不会为模板类的方法创建内容。 However, this may pose problems when you're designing large template class (because you're very likely to forget to add test call to new method somewhere). 但是,当您设计大型模板类时,这可能会出现问题(因为您很可能忘记在某处添加测试调用到新方法)。

The question: 问题:
Is there a compiler switch for g++ or cl.exe that would force compiler to process entire template (so this code fragment will trigger compilation error)? 是否有g ++或cl.exe的编译器开关会强制编译器处理整个模板(因此这段代码片段会触发编译错误)?

If you want to test the template with a couple of types, you can trigger manual instantiations of the types, as in: 如果要使用几种类型测试模板,可以触发类型的手动实例化,如:

// at namespace level
template class Test<int>;

Explicit instantiations of class templates automatically trigger the instantiation of all members, which seems to be what you want. 类模板的显式实例化会自动触发所有成员的实例化,这似乎就是您想要的。

The actual problem is that the language is designed to explicitly allow the behavior that you want to avoid. 实际问题是该语言旨在明确允许您要避免的行为。 When a class template is implicitly instantiated, the compiler will instantiate only those methods that are used . 当一个类模板隐式实例化时,编译器将实例只有那些使用的方法。 The main use case of the feature is that some methods might impose stricter requirements on the instantiating type than others, if all methods were instantiated always then the class template could only be used with those types that fulfill the stricter requirements. 该功能的主要用途的情况是,有些方法可能对实体化类型强制规定严格的要求比别人,如果所有方法中实例总是那么类模板只能与那些满足更严格的要求类型使用。

By allowing the compiler to only instantiate those methods that are used, the class template can be used with types that don't meet all of the requirements of all of the methods, as long as they meet the requirements of the methods that are actually used. 通过允许编译器仅实例化所使用的那些方法,类模板可以与不满足所有方法的所有要求的类型一起使用,只要它们满足实际使用的方法的要求即可。 。

A common example is operator[] in std::map<> that requires the value_type to be default-constructible ( operator[] will create a new object default initialized if the key is not present in the container and return a reference to it). 一个常见的例子是std::map<>中的operator[] ,它要求value_type是可默认构造的 (如果容器中没有键并且返回对它的引用, operator[]将创建一个新的对象默认初始化 ) 。 The behavior in the language allows you to use std::map on types that are not default-constructible as long as you don't use operator[] (or any other member function that imposes that requirement). 只要您不使用operator[] (或任何其他强制要求的成员函数),语言中的行为允许您对不可默认构造的类型使用std::map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM