成员函数类型推导 [英] member function type deduction

查看:133
本文介绍了成员函数类型推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

我想推断作为参数传递给函数模板的成员函数的类型.

当将指向成员函数的指针作为参数传递给函数模板时,我可以推断出成员函数的参数类型,而不是成员函数类型!

有人知道我该怎么做吗?


Hi.

I want to deduce the type of a member function passed as an argument to a function template.

I can deduce the types of the arguments of a member function when a pointer to the member function is passed as an argument to a function template, but not the member function type!

Anyone know how I can do this ?


// Deduction.h
#include<vector>


// Test Function
bool TestFun(int X)
{
	std::cout << "TestFun\n";
	return true;
};


// Test Component
struct ComponentB
{
	int Round(float X)
	{
		int x = (int)X;
		std::cout << "Round " << X << " = " << x << "\n";
		return x;
	}
};


// template type deduction from function arguments of function pointer
template<typename R, typename P1>
void Deduce( R(*pFunc)(P1) )
{
	// pFunc is a pointer to a function, but have we been able to deduce R and P1 ?
	R MyR;
	P1 MyParam1;

	MyR = pFunc(MyParam1);
	return;
};


// template type deduction from function arguments of member function pointer
template<typename R, typename T, typename P1>
void DeduceMFA( R(T::*pFunc)(P1) )
{
	// pFunc is a pointer to a Member function, but have we been able to deduce R, T and P1 ?
	R MyR;
	P1 MyParam1;
	T MyTarget;

	MyR = (MyTarget.*pFunc)(MyParam1);
	return;
};


// but we cannot use pFunc as a template argument for a templated type because it is a "local variable"!
template<typename R, typename T, typename P1>
void DeduceMFB( R(T::*pFunc)(P1) )
{
	// pFunc is a pointer to a Member function, but have we been able to deduce R, T and P1 ?
	R MyR;
	P1 MyParam1;
	T MyTarget;

	std::vector<pfunc> MyFuncts;	// Error pFunc is a local variable. Can''t be used as template argument.

	MyR = (MyTarget.*pFunc)(MyParam1);
	return;
};


// so we implement it as a full template type
template<typename R, typename T, typename P1, typename R(T::*pFunc)(P1)>
void DeduceMFC( R(T::*pFunc)(P1) )
{
	// pFunc is a pointer to a Member function, but have we been able to deduce R, T and P1 AND  pFunc ?
	R MyR;
	P1 MyParam1;
	T MyTarget;

	MyR = (MyTarget.*pFunc)(MyParam1);
	return;
};


// code from main.cpp

// but then it cannot be deduced!!!

	Deduce( &TestFun );			// OK

	DeduceMFA( &ComponentB::Round );	// OK

	DeduceMFB( &ComponentB::Round );	// Error. ''pFunc'' is not a valid template type argument for parameter ''_Ty''

	DeduceMFC( &ComponentB::Round );	// Error. could not deduce template argument for ''pFunc''


任何帮助表示赞赏.

(由Stefan63撰写)修复了模板头(希望我正确设置了它们)[/edit]


Any help appreciated.

[edit] (by Stefan63) fixed the template headers (hope I got them right)[/edit]

推荐答案

我对这些行感到有些困惑:
I am slightly confused by these lines:
template<typename typename="" p1="">


据我所知,这不是有效的C ++,它可能是新的C ++ 0x的一部分,因为我还没有时间详细审查它.但是无论如何,您都不需要使用任何新功能来解决此问题,因此我将继续进行.

因此,首先,要指定所有模板参数,我们每个参数都需要一个类型名,并将它们放入逗号分隔的列表中.请注意,P1的大小写必须与您的函数原型匹配.


As far as I am aware this is not valid C++, it could be part of new C++0x stuff as I haven''t had time to review that in detail yet. But anyway you shouldn''t require to use any new features to solve this problem so I will proceed anyway.

So firstly, to specify all the template arguments we need one typename for each and make them into a comma separated list. Notice the capitalisation on the P1 as the case must match your function prototype.

template<typename  R, typename T, typename P1>



好的,从头开始,就像我刚刚注意到的那样,这似乎是该网站的问题.

接下来让我们看一下矢量定义:



Ok scratch that as I just noticed it seems that is a problem with the site.

Next lets look at that vector definition:

std::vector<pfunc> MyFuncts;


它不起作用的原因是,正如您所说的pfunc是局部变量,它是传递给我们函数的参数,并且模板参数必须是类型.使用我们的模板参数和typedef构造与pfunc类型匹配的类型很简单.结合正确的模板规范可以得到以下代码:


The reason it won''t work is because as you say pfunc is a local variable, it is the argument passed to our function and template arguments must be types. It is simple to construct a type that matches the type of pfunc however using a our template arguments and a typedef. Which put together with the correct template specification gives us the following code:

template<typename  R, typename T, typename P1>
void DeduceMFB( R(T::*pFunc)(P1) )
{
    R MyR;
    P1 MyParam1;
    T MyTarget;

    typedef R (T::*pNowATypeFunc)(P1);
    std::vector<pNowATypeFunc> MyFuncts;

    MyR = (MyTarget.*pFunc)(MyParam1);
    return;
};



现在,由于我不太遵循您使用的模板符号,因此我发现3个DeduceMF函数之间的区别不大,除了包含向量的DeduceMFB,所以我希望我不会错过任何事情,但是这个函数在这三种情况下都应该工作愉快.希望对您有所帮助.



Now as I don''t quite follow the template notation you have used I don''t see much difference between the 3 DeduceMF functions well except for DeduceMFB including a vector, so I hope I didn''t miss anything, but this function should work happily in all three cases. Hope that helps.


第一个错误很简单:您将变量用作模板参数,但所需的是该变量的类型.我对函数指针不是很熟悉,但是最简单的解决方案可能是插入定义pfunc类型的typedef,然后使用该typename作为向量的参数.

我没有得到的第二个错误:DeduceMFC的代码是DeduceMFA的精确副本.唯一的区别是模板参数列表,而TBH我不知道它是什么语法.

您的模板参数列表确实是这样吗?还是格式混乱?如果它是正确的,那是什么意思?
[edit]修复了原始帖子中的模板头-我希望我没错,但是现在DeduceMFBDeduceMFC之间没有任何区别!?
[edit2]再次修复了模板参数以匹配修订版1中发布的代码-显然,重新格式化导致了一些无意的更改... [/edit2]
The first error is simple: you used a variable as a template argument, but what you needed is the type of that variable. I''m not so familiar with function pointers, but the simplest solution will probably be to insert a typedef that defines the type of pfunc, and then use that typename as argument for your vector.

The second error I don''t get: the code of DeduceMFC is an exact copy from DeduceMFA. The only difference is the template argument list, and TBH I have no idea what syntax that is.

Does your template argument list really look like this or was it messed up in formatting? If it''s correct like it is, what does it mean?
[edit] fixed the template headers in the original post - I hope I got them right, but now there''s no difference at all between DeduceMFB and DeduceMFC!?
[edit2] fixed the template arguments again to match the code posted in revision 1 - obviously the reformatting caused some inadvertent changes... [/edit2]


好,我检查了修订版1您的问题以了解为什么DeduceMFBDeduceMFC之间应该存在差异.我发现,与更高版本中显示的内容不同,后者具有第四个模板参数!

但是,第四个参数根本没有任何意义,很明显为什么在那里会出现错误.

我不太确定您要使用第四个参数实现什么目标.也许您可以对此进行详细说明,以便我们了解其中的内容,以及是否有可能将其合理地整合到模板中?
Ok, I checked on Revision 1 of your question to see why there is supposed to be a difference between DeduceMFB and DeduceMFC. I''ve found, that unlike what is shown in the later revisions, the latter sports a fourth template parameter!

This fourth parameter however doesn''t make any sense at all, and it is obvious why you get an error there.

I''m not quite sure what you meant to achieve with that fourth parameter. Maybe you can elaborate on that, so we can see what there is to it, and if it''s even possible to sensibly incorporate this into the template?


这篇关于成员函数类型推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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