重载/专门在简历预选赛 [英] overload/specialize on cv qualifier

查看:101
本文介绍了重载/专门在简历预选赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为多线程委托连接系统创建具有两个实现的模板(非成员)函数.
选择的实现需要由最后一个模板参数的cv限定符确定,该参数需要一个(非类型)成员函数指针类型.

这听起来像是重载,但是在函数参数列表中未使用template参数,因此这并不适用.
可以通过专业化来解决,只是(非类型)成员函数指针类型不是唯一的模板参数,并且模板函数不支持部分专业化!

这是一个示例:

I need to create a template (non-member) function with two implementations for a multi-threaded delegate connection system.
The chosen implementation needs to be determined by the cv qualifier of the last template parameter which expects a (non-type) member function pointer type.

This sounds like overloading, but the template parameter is not used in the function parameter list so this doesn''t apply.
It might be solved by specialization except that the (non-type) member function pointer type is not the only template parameter and template functions don''t support partial specialization!

Here''s an example:

// Define Source with 1 parameter
template<typename R, typename P>
struct Source
{
	typedef R (*FunctionType) (void*, P);
	FunctionType pRetargetingFunction;
	void* m_pTarget;

	// Call the Source....which calls the retargeting function....which calls the target function
	R operator()(P a)
	{
		return pRetargetingFunction( m_pTarget, a );
	}
};



// Define a Targeter with 1 parameter which defines the ReTargeting Function
template<typename R, typename T, typename P, R (T::*TargetFunction)(P)>
struct Targeter
{
	// define the static Retargeting Function
	static R RetargetFunction( void* pTarget, P a)
	{
		// Store the Result
		R Result;
		T* pTargetObject = static_cast<t*>(pTarget);

		// Lock the target
		pTargetObject->Lock();	// ConstTargeter calls pTargetObject->Shared_Lock();

		// Call the target function   
		Result = (pTargetObject->*TargetFunction)(a);

		// Unlock the target
		pTargetObject->Unlock();

		// Return Result
		return Result;
	}
};



// Connect for non-const Member Function
template<typename R, typename T, typename P, typename R (T::*TargetFunction)(P)>
void Connect(Source<r,>* pSource, T* pTarget)
{
	pSource->m_pTarget = pTarget;
	pSource->m_pRetargetingFunction = &Targeter<r,>::RetargetFunction;
	return;
};





它们仅在最后一个模板参数的恒定性和用于生成源重新定向"功能的Targeter类型上有所不同(它们实现了不同的锁定策略:非const成员函数的排他锁定和const成员函数的共享锁定). br/>
我发现的唯一解决方法是使用不同的函数名称(Connect和ConstConnect),但是我不希望代码的用户不得不查找目标成员函数的常数. />
我已经看过boost :: Is_const结构,但无法理解如何使typedef/selector使用结果(并且不知道它是否可以处理a的const-ness).成员函数指针类型).

任何人都有追求的解决方案或想法?





They differ only in the const-ness of the last template parameter and the type of Targeter used to generate the Source Retargeting function (which implement different locking policies: exclusive locking for non-const member functions and shared locking for const member functions).

The only workaround I have found is to use different function names (Connect and ConstConnect), but I don''t want the user''s of the code to have to look up the const-ness of the targeted member function.

I''ve looked at the boost::Is_const struct, but can''t get my head around how to make a typedef/selector to use the result (and don''t know if it will handle the const-ness of a member function pointer type).

Anyone have a solution or ideas to pursue ?

推荐答案

您的源指针类型必须取决于目标指针类型,因为pRetargetingFunction取决于目标指针类型.显式强制转换对模板指针没有用.
例如:
Your source pointer type must depend on the target pointer type because the pRetargetingFunction depends on the target pointer type. An explicit cast is not useful for template pointers.
example:
#pragma once
#include <stdio.h>
#include <tchar.h>

template <typename T>
class Targeter
{
public:
  static void  RetargetFunction(T* p,const TCHAR* name)
  {
    _tprintf(__T("Targeter(%s)\r\n"),name);
  }
};

template <typename T>
class ConstTargeter
{
public:
  static void  RetargetFunction(const T* p,const TCHAR* name)
  {
    _tprintf(__T("ConstTargeter(%s)\r\n"),name);
  }
};

/////////////////////////////////////////////
// Connect for non-const Member Function template
template <typename Source,typename T>
void Connect(Source* pSource, T* pTarget)
{
  pSource->m_pTarget = pTarget;
  pSource->pRetargetingFunction = &Targeter<T>::RetargetFunction;
  return;
}

// Connect for const Member Function template
template <typename Source,typename T>
void Connect(Source* pSource,const T* pTarget)
{
  pSource->m_pTarget = pTarget;
  pSource->pRetargetingFunction = &ConstTargeter<T>::RetargetFunction;
  return;
}
/////////////////////////////////////////////

template <class T>
class tSourceA
{
  typedef void  (*RETARGET)(T*,const TCHAR*);
  static void  intern(T* p,const TCHAR* name)
  {
    _tprintf(__T("internA(%s)\r\n"),name);
  }
public:
  tSourceA(){ m_pTarget=0; pRetargetingFunction=&tSourceA::intern; }
  ~tSourceA(){ pRetargetingFunction(m_pTarget,__T("tSourceA")); }
  T*        m_pTarget;
  RETARGET  pRetargetingFunction;
};

template <class T>
class tSourceB
{
  typedef void  (*RETARGET)(T*,const TCHAR*);
  static void  intern(T* p,const TCHAR* name)
  {
    _tprintf(__T("internB(%s)\r\n"),name);
  }
public:
  tSourceB(){ m_pTarget=0; pRetargetingFunction=&tSourceB::intern; }
  ~tSourceB(){ pRetargetingFunction(m_pTarget,__T("tSourceB")); }
  T*        m_pTarget;
  RETARGET  pRetargetingFunction;
};

int _tmain(int argc, _TCHAR* argv[])
{
  { // example A: not const
    tSourceA<int>    x;
    int              ai[]={1,2,3};

    Connect(&x,ai);
  }
  { // example A: with const
    tSourceA<const int>    x;
    const int              ai[]={1,2,3};

    Connect(&x,ai);
  }
  { // example B: not const
    tSourceB<char>        x;
    char                  ai[]={'1','2','3'};

    Connect(&x,ai);
  }
  { // example B: with const
    tSourceB<const char>  x;
    const char            ai[]={'1','2','3'};

    Connect(&x,ai);
  }

  _gettch();  
  return 0;
}


结果:


result:

<br />
Targeter(tSourceA)<br />
ConstTargeter(tSourceA)<br />
Targeter(tSourceB)<br />
ConstTargeter(tSourceB)<br />


您不能将const源类与const目标类型混合使用,反之亦然.因为重定向功能取决于目标类型.
我希望我能帮上忙.
问候.


You cannot mix none const source classes with const target types and vice versa. Because the retargeting function depends on the target type.
i hope i could help.
regards.


这篇关于重载/专门在简历预选赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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