防止模板化函数的多个实例化 [英] Preventing multiple instantiations of a templated function

查看:58
本文介绍了防止模板化函数的多个实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


是否有可能阻止_at编译或链接time_多次实例化

模板化函数?换句话说,如果存在函数


模板< typename Tvoid fn();


我想阻止用户做这个:


int main()

{

fn< int>();

FN<双>(); //< - 应该导致编译时间或链接时间错误

}


重要的是整个<只有一个函数实例br />
计划。无论哪种类型被实例化都没关系,但是不能用两个不同类型参数对函数进行两次引用。


如果这是可能的,那么有没有办法为_any_模板

实体做这个?


-dr

Hi all,

Is it possible to prevent _at compile or link time_ the mulitple instantiation
of a templated function? In other words, if there exists a function

template <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 entire
program. It doesn''t matter which type becomes instantiated, but there cannot
be two references to the function with two different type parameters.

If this is possible, then is there a way to do this for _any_ templated
entity?

-dr

推荐答案

Dave Rahardja写道:
Dave Rahardja wrote:

大家好,


是否有可能阻止_at编译或链接时间_多元实例化

的模板化函数?换句话说,如果存在函数


模板< typename Tvoid fn();


我想阻止用户做这个:


int main()

{

fn< int>();

FN<双>(); //< - 应该导致编译时间或链接时间错误

}


重要的是整个<只有一个函数实例br />
计划。无论哪种类型被实例化都没关系,但是不能使用两个不同类型参数对函数进行两次引用。
Hi all,

Is it possible to prevent _at compile or link time_ the mulitple instantiation
of a templated function? In other words, if there exists a function

template <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 entire
program. It doesn''t matter which type becomes instantiated, but there cannot
be two references to the function with two different type parameters.



函数模板的每个实例都是一个独特的函数,因此

的答案可能不是。


你为什么要这样做?


-

Ian Collins。

Each instance of the function template is a unique function, so the
answer is probably no.

Why do you want to do this?

--
Ian Collins.


Ian Collins写道:
Ian Collins wrote:

Dave Rahardja写道:
Dave Rahardja wrote:

>我想阻止用户这样做:

int main()
{
fn< int>();
fn< double>(); //< - 应该导致编译时间或链接时间
错误



整个程序中只有一个函数实例很重要。无论哪种类型实例化都没有关系,但是对于具有两个不同类型参数的函数
不能有两个引用。
>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
entire program. It doesn''t matter which type becomes
instantiated, but there cannot be two references to the function
with two different type parameters.



函数模板的每个实例都是一个独特的函数,所以

的答案可能是否定的。

Each instance of the function template is a unique function, so
the answer is probably no.



我想补充说即使你可以在你的整个程序中强制执行单个* special $ *
模板,也有机会(取决于

编译器的质量)可以生成

专业化的倍数*实例*。

干杯,

-

IR

I''d add that even if you could enforce a single *specialization* of
the template in your whole program, there are chances (depending on
the quality of your compiler) that multiples *instances* of the
specialization could be generated.
Cheers,
--
IR


On Sun,2007年1月21日09:14:12 +1300,Ian Collins< ia******@hotmail.com写道:
On Sun, 21 Jan 2007 09:14:12 +1300, Ian Collins <ia******@hotmail.comwrote:

> Dave Rahardja写道:
>Dave Rahardja wrote:

>大家好,

是否有可能阻止_at编译或链接time_多次实例化模板化函数?换句话说,如果存在一个函数

模板< typename Tvoid fn();

我想阻止用户这样做:

int main()
{
fn< int>();
fn< double>(); //< - 应该导致编译时间或链接时间错误


重要的是整个
程序中只有一个函数实例。无论哪种类型被实例化都没关系,但是不能对具有两个不同类型参数的函数进行两次引用。
>Hi all,

Is it possible to prevent _at compile or link time_ the mulitple instantiation
of a templated function? In other words, if there exists a function

template <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 entire
program. It doesn''t matter which type becomes instantiated, but there cannot
be two references to the function with two different type parameters.


函数模板的每个实例都是独特的功能,所以
答案可能没有。

你为什么要这样做?

Each instance of the function template is a unique function, so the
answer 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;

返回资源; < br $>
}


//预订政策

class Policy0 {/ * ... * /};

class Policy1 {/ * ... * /};


int main()

{

fn< Policy0> ;(); //好的

fn< Policy0>(); //好的

fn< Policy1>(); //运行时断言

}

但是我想知道我是否可以在_compile或link_时阻止这个问题。


-dr

I 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 in
terms of some ReservationPolicy.

Problem is, it''s an error to have the same resource accessed via a multitude
of ReservationPolicy''s, because the accessor provides access to a single
underlying resource. A decision must be made at program design time as to what
ReservationPolicy that one resource is supposed to use. Once that decision is
made, 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 policies
class Policy0 { /* ... */ };
class Policy1 { /* ... */ };

int main()
{
fn<Policy0>(); // OK
fn<Policy0>(); // OK
fn<Policy1>(); // Runtime assertion
}
But I''d like to know if I can prevent this problem at _compile or link_ time.

-dr


这篇关于防止模板化函数的多个实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆