本文介绍了防止模板化函数的多个实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 是否有可能阻止_at编译或链接time_多次实例化 模板化函数?换句话说,如果存在函数 模板< typename Tvoid fn(); 我想阻止用户做这个: int main() { fn< int>(); FN<双>(); //< - 应该导致编译时间或链接时间错误 } 重要的是整个 计划。无论哪种类型被实例化都没关系,但是不能用两个不同类型参数对函数进行两次引用。 如果这是可能的,那么有没有办法为_any_模板 实体做这个? -drHi all,Is it possible to prevent _at compile or link time_ the mulitple instantiationof a templated function? In other words, if there exists a functiontemplate <typename Tvoid fn();I want to prevent the user from doing this:int main(){fn<int>();fn<double>(); // <-- should cause compile time or link time error}It matters that there is only one instance of the function in the entireprogram. It doesn''t matter which type becomes instantiated, but there cannotbe two references to the function with two different type parameters.If this is possible, then is there a way to do this for _any_ templatedentity?-dr推荐答案 函数模板的每个实例都是一个独特的函数,因此 的答案可能不是。 你为什么要这样做? - Ian Collins。Each instance of the function template is a unique function, so theanswer is probably no.Why do you want to do this?--Ian Collins. 函数模板的每个实例都是一个独特的函数,所以 的答案可能是否定的。Each instance of the function template is a unique function, sothe answer is probably no. 我想补充说即使你可以在你的整个程序中强制执行单个* special $ * 模板,也有机会(取决于 编译器的质量)可以生成 专业化的倍数*实例*。 干杯, - IRI''d add that even if you could enforce a single *specialization* ofthe template in your whole program, there are chances (depending onthe quality of your compiler) that multiples *instances* of thespecialization could be generated.Cheers,--IR 函数模板的每个实例都是独特的功能,所以答案可能没有。 你为什么要这样做?Each instance of the function template is a unique function, so theanswer is probably no.Why do you want to do this? 我在这个表格的库中有一组访问者: 模板< class ReservationPolicy> 类资源:私人ReservationPolicy {// ... }; 模板< class ReservationPolicy> 资源& getResource() { 静态资源< ReservationPolicyresource; 返回资源; } 这是一个资源的简单Singleton访问器,用一些ReservationPolicy的 条款实现。 问题是,它是一个因为访问者提供对单个 底层资源的访问权限,所以错误是通过多个来访问相同的资源 的ReservationPolicy'。必须在程序设计时决定一个资源应该使用什么 ReservationPolicy。一旦做出决定是,调用getResource()的另一个实例是错误的。 我可以通过以下方式阻止_runtime_的问题: br /> #include< cassert> //资源。 模板< class ReservationPolicy> class资源:private ReservationPolicy {/ * ... * /}; struct ResourceTag {}; //帮助强制执行一个实例化规则的助手类。 模板< typename T> 类OneInstance { public: OneInstance() {assert(++ instanceCount == 1); } 私人: static int instanceCount; }; 模板< typename Tint OneInstance< T> :: instanceCount = 0; //访问者函数。只有一个这个函数的实例可以被调用而不会产生运行时错误。 模板< ReservationPolicy> 资源& getResource() { 静态OneInstance< ResourceTagoneInstance; 静态资源< ReservationPolicyresource; 返回资源; } //预订政策 class Policy0 {/ * ... * /}; class Policy1 {/ * ... * /}; int main() { fn< Policy0> ;(); //好的 fn< Policy0>(); //好的 fn< Policy1>(); //运行时断言 } 但是我想知道我是否可以在_compile或link_时阻止这个问题。 -drI have a set of accessors in a library of this form:template <class ReservationPolicy>class Resource: private ReservationPolicy{ // ...};template <class ReservationPolicy>Resource& getResource(){static Resource<ReservationPolicyresource;return resource;}It''s a simplistic Singleton accessor for a Resource that is implemented interms of some ReservationPolicy.Problem is, it''s an error to have the same resource accessed via a multitudeof ReservationPolicy''s, because the accessor provides access to a singleunderlying resource. A decision must be made at program design time as to whatReservationPolicy that one resource is supposed to use. Once that decision ismade, it is an error to call a different instance of getResource().I can prevent the problem at _runtime_ by:#include <cassert>// The resource.template <class ReservationPolicy>class Resource: private ReservationPolicy{ /* ... */ };struct ResourceTag {};// Helper class that helps enforce a one-instantiation rule.template <typename T>class OneInstance{public:OneInstance(){ assert(++instanceCount == 1); }private:static int instanceCount;};template <typename Tint OneInstance<T>::instanceCount = 0;// Accessor function. Only one instance of this function can be// called without generating a runtime error.template <ReservationPolicy>Resource& getResource(){static OneInstance<ResourceTagoneInstance;static Resource<ReservationPolicyresource;return resource;}// Reservation policiesclass Policy0 { /* ... */ };class Policy1 { /* ... */ };int main(){fn<Policy0>(); // OKfn<Policy0>(); // OKfn<Policy1>(); // Runtime assertion}But I''d like to know if I can prevent this problem at _compile or link_ time.-dr 这篇关于防止模板化函数的多个实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 05:18