解析模板参数 [英] Resolving template parameters
问题描述
我正在寻找实现以下目标的方法。我已经尝试了几个
的东西,但它们都变得太复杂了:
我有一个模板化的A级。我想要另一个课程B能够调用在A'基类中定义的
a方法,该方法在运行时确定
模板参数(我知道允许的是什么)并调用
模板化成员函数B,带有A'的模板参数。
我想象这样的东西 - 我知道它不能像
这个,但我想实现一个类似的简单语法:
class Base
{
public:
虚拟空调(...)= 0;
}
模板<类T1, T2级>
A级:公共基地
{
公开:
无效通话(...)
{
if(typeid(T1)== ...)
...
else if (...)
...
否则如果(...)
...
}
}
B级
{
公开:
void x()
{
Base * pSomePtr = ...;
pSomePtr-> call(this,& B :: y);
};
模板<类T1,T2类>
void y()
{
//做东西
};
};
关于如何实现这一点的任何帮助非常感谢。
谢谢,
Stefan
< blockquote> st ************* @ gmail.com 写道:
我正在寻找一种方法来实现以下目标。我已经尝试了几个
的东西,但它们都变得太复杂了:
我有一个模板化的A级。我想要另一个课程B能够调用在A'基类中定义的
a方法,该方法在运行时确定
模板参数(我知道允许的是什么)并调用
模板化成员函数B,带有A'的模板参数。
我想象这样的东西 - 我知道它不能像
这个,但我想实现一个类似的简单语法:
class Base
{
public:
虚拟空调(...)= 0;
}
模板<类T1, T2级>
A级:公共基地
{
公开:
无效通话(...)
{
if(typeid(T1)== ...)
...
else if (...)
...
if if(。 ..)
...
}
}
B级
{
public:
void x()
{
Base * pSomePtr = ...;
pSomePtr-> call(this,& B :: y);
};
模板<类T1,类T2>
void y()
{
//做东西
};
};
任何有关如何实现这一点的帮助将不胜感激。
谢谢,
Stefan
这对我有用,并且它不会被限制在一组已知的模板中
参数。但它没有将要调用的成员函数传递给
调用方法。我不确定这是否属于你的要求。
john
#include< iostream>
>
B级;
等级基础
{
公开:
虚拟void call(B * b)= 0;
};
class B
{
public:
void x(Base * pSomePtr)
{
pSomePtr-> call(this);
};
模板<类T1,T2类>
void y()
{
std :: cout<< typeid(T1).name()<< ''''<< typeid(T2).name()<< ''\\ n';;
};
};
模板<类T1,类T2>
A级:公共基地
{
公开:
无效通话(B * b)
{
b-> y< T1,T2>();
}
};
int main()
{
A< int,doublea;
B b;
bx(& a);
}
John Harrison写道:
>
这对我有用,并且它不会被限制为一组已知的模板
参数。但它没有将要调用的成员函数传递给
调用方法。我不确定这是否属于你的要求。
john
#include< iostream>
B级;
等级基础
{
公开:
虚拟空调(B * b)= 0;
};
B级
{
public:
void x(Base * pSomePtr)
{
pSomePtr-> call(this );
};
模板<类T1,T2类>
void y()
{
std :: cout<< typeid(T1).name()<< ''''<< typeid(T2).name()<< ''\\ n';;
};
};
模板<类T1,类T2>
A级:公共基地
{
公开:
无效通话(B * b)
{
b-> y< T1,T2>();
}
};
int main()
{
A< int,doublea;
B b;
bx(& a);
}
Darn,你打败了我! :)有趣的是,我们用相同的解决方案提出了
。顺便说一句,我忘记了(所以我去了,包括
在我的解决方案中)你需要做什么 - >模板在电话中
到成员函数y()? (见下文)。
------------------------------------- -------------------------
//自
修改你的例子并删除省略号//对我来说太模糊了。 WLOG,
//假设你的所有函数都有无效参数。
//需要转发声明。
B类;
//好东西在这里。保持
等级基础
{
公开:
//我推断你想要申请成员函数
// B类实例
虚拟空调(B * b)= 0;
}
//由于依赖性而不得不将B的定义移动。
B级
{
public:
void x();
模板<类T1,类T2>
void y()
{
T1 * a =新T1();
T2 * b =新T2();
//泄漏内存!!哇哦!
};
};
模板<类T1,T2级>
级A:公共基地
{
公共:
//这需要虚拟吗?
虚拟空调(B * b)
{
//你不需要打开typeid。因为虚拟的
//函数会自动为你做这个我也假设这个调用这个调用。函数将调用y。
//如果你需要将T1和T2重新映射到其他类型
//不要打开typeid,而是使用模板类型函数。
b->模板y< T1,T2>();
}
}
inline void B :: x()
{
Base * pSomePtr = new A< int,float>();
pSomePtr - >打电话(这个);
// WooHoo !!泄漏更多记忆!!
};
//老实说,这种循环依赖关系
// A类和B,你确定你想要这样做吗?
2月21日上午8:08,John Harrison< john_androni ... @ hotmail.comwrote :
stefan.bruck ... @ gmail.com写道:
我正在寻找一种方法来实现以下目标。我已经尝试了几个
的东西,但它们都变得太复杂了:
我有一个模板化的A类。我希望另一个B类能够调用在A'基类中定义的
a方法,该方法在运行时确定了
模板参数(我知道了)什么是允许的)并使用A'的模板参数调用
模板化成员函数B.
我想象这样的东西 - 我知道它不能像
这样工作,但是我想实现一个类似的简单语法:
class Base
{
public :
虚拟空调(...)= 0;
}
模板< class T1,class T2>
class A:public Base
{
public:
void call (...)
{
if(typeid(T1)== ...)
...
否则如果(...)
...
否则如果(...)
...
}
}
class B
{
public:
void x()
{
Base * pSomePtr = ...;
pSomePtr-> call(this,& B :: y);
};
template< class T1,class T2>
void y()
{
//做东西
};
};
任何有关如何实现这一点的帮助将不胜感激。
谢谢,
Stefan
这适用于我,它并没有被限制在一组已知的模板
参数中。但它没有将要调用的成员函数传递给
调用方法。我不确定这是否属于你的要求。
john
#include< iostream>
B级;
等级基础
{
公开:
虚拟空调(B * b)= 0;
};
B级
{
public:
void x(Base * pSomePtr)
{
pSomePtr-> ;打电话(这);
};
模板<类T1,类T2>
void y()
{
std :: cout<< typeid(T1).name()<< ''''<< typeid(T2).name()<< ''\\ n';;
};
};
模板<类T1, T2级>
A级:公共基地
{
公开:
无效通话(B * b)
{
b-> y< T1,T2>();
}
};
int main()
{
A< int,doublea;
B b;
bx(& a);
}
感谢您的支持答复。对不起,我忘了提这个,但不幸的是,这是指定要调用的成员
函数的关键要求之一,所以你可以这样做:
B级
{
公开:
void x(Base * pSomePtr ,Base * pSomeOtherPtr)
{
pSomePtr-> call(this,& B :: dosomething);
pSomePtr-> call(this,& B :: dosomethingelse);
pSomeOtherPtr-> call(this,& B :: doanotherthing);
};
模板<类T1,T2类>
无效dosomething()
{
std :: cout<< typeid(T1).name()<< ''''<<
typeid(T2).name()<< ''\ n'';
};
模板<类T1,T2类>
无效dosomethingelse( )
{
std :: cout<< typeid(T1).name()<< ''''<<
typeid(T2).name()<< ''\\ n';;
};
模板<类T1,T2级>
无效的doanotherthing( )
{
std :: cout<< typeid(T1).name()<< ''''<<
typeid(T2).name()<< ''\ n'';
};
};
如果不是那样的话,我猜猜你的解决方案是理想的。我想要的问题是什么 - 当然 - 使用
成员函数指针是不可能的。所以我正在寻找能够提供类似优秀语法的东西。
--Stefan
Hi,
I am looking for a way to achieve the following. I''ve tried a couple
of things, but they all ended up being too complicated:
I have a templated class A. I want another class B to be able to call
a method defined in A''s base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A''s template parameters.
I''m imagining something like this - I know that it can''t work like
this, but I would like to achieve a similarly simple syntax:
class Base
{
public:
virtual void call(...) = 0;
}
template <class T1,class T2>
class A : public Base
{
public:
void call(...)
{
if (typeid(T1) == ...)
...
else if (...)
...
else if (...)
...
}
}
class B
{
public:
void x()
{
Base *pSomePtr = ...;
pSomePtr->call(this,&B::y);
};
template <class T1,class T2>
void y()
{
// do stuff
};
};
Any help on how I could realize this would be greatly appreciated .
Thanks,
Stefan
st*************@gmail.com wrote:Hi,
I am looking for a way to achieve the following. I''ve tried a couple
of things, but they all ended up being too complicated:
I have a templated class A. I want another class B to be able to call
a method defined in A''s base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A''s template parameters.
I''m imagining something like this - I know that it can''t work like
this, but I would like to achieve a similarly simple syntax:
class Base
{
public:
virtual void call(...) = 0;
}
template <class T1,class T2>
class A : public Base
{
public:
void call(...)
{
if (typeid(T1) == ...)
...
else if (...)
...
else if (...)
...
}
}
class B
{
public:
void x()
{
Base *pSomePtr = ...;
pSomePtr->call(this,&B::y);
};
template <class T1,class T2>
void y()
{
// do stuff
};
};
Any help on how I could realize this would be greatly appreciated .
Thanks,
Stefan
This works for me, and it isn''t retricted to a known set of template
parameters. But it doesn''t pass the member function to be called to the
call method. I wasn''t sure if that was part of your requirements or not.
john
#include <iostream>
class B;
class Base
{
public:
virtual void call(B* b) = 0;
};
class B
{
public:
void x(Base *pSomePtr)
{
pSomePtr->call(this);
};
template <class T1,class T2>
void y()
{
std::cout << typeid(T1).name() << '' '' << typeid(T2).name() << ''\n'';
};
};
template <class T1, class T2>
class A : public Base
{
public:
void call(B* b)
{
b->y<T1, T2>();
}
};
int main()
{
A<int, doublea;
B b;
b.x(&a);
}
John Harrison wrote:>
This works for me, and it isn''t retricted to a known set of template
parameters. But it doesn''t pass the member function to be called to the
call method. I wasn''t sure if that was part of your requirements or not.
john
#include <iostream>
class B;
class Base
{
public:
virtual void call(B* b) = 0;
};
class B
{
public:
void x(Base *pSomePtr)
{
pSomePtr->call(this);
};
template <class T1,class T2>
void y()
{
std::cout << typeid(T1).name() << '' '' << typeid(T2).name() << ''\n'';
};
};
template <class T1, class T2>
class A : public Base
{
public:
void call(B* b)
{
b->y<T1, T2>();
}
};
int main()
{
A<int, doublea;
B b;
b.x(&a);
}Darn, you beat me to it!! :) Interestingly enough, we came up
with the same solution. BTW, I forgot (so I went and included
it in my solution) do you need to do ->template in the call
to member function y()? (See below).
--------------------------------------------------------------
// Modifying your example and removing ellipsis since
// it is too vague for me to work with. WLOG,
// assuming all your functions are have void arguments.
// forward declaration required.
class B;
// Good Stuff here. Keep
class Base
{
public:
// I am deducing you want this to apply a member function to
// an instance of class B
virtual void call( B *b ) =0;
}
// had to move the definition of B up due to dependency.
class B
{
public:
void x();
template <class T1,class T2>
void y()
{
T1 *a = new T1();
T2 *b = new T2();
// leak memory!! WooHoo!
};
};
template <class T1,class T2>
class A : public Base
{
public:
// this needs to be virtual right?
virtual void call( B *b )
{
// you do not need to switch on typeid. since the virtual
// function will do it for you automagically I am also assuming
// that this "call" function will call y.
// if you need to remap T1 and T2 to some other type
// do NOT switch on typeid, use a template type function instead.
b->template y<T1,T2>();
}
}
inline void B::x()
{
Base *pSomePtr = new A<int, float>();
pSomePtr->call( this );
// WooHoo!! Leak more memory!!
};
// Honestly though, with such a cyclic dependency between
// class A and B, are you sure you want to do this at all?
On Feb 21, 8:08 am, John Harrison <john_androni...@hotmail.comwrote:stefan.bruck...@gmail.com wrote:Hi,
I am looking for a way to achieve the following. I''ve tried a couple
of things, but they all ended up being too complicated:
I have a templated class A. I want another class B to be able to call
a method defined in A''s base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A''s template parameters.
I''m imagining something like this - I know that it can''t work like
this, but I would like to achieve a similarly simple syntax:
class Base
{
public:
virtual void call(...) = 0;
}
template <class T1,class T2>
class A : public Base
{
public:
void call(...)
{
if (typeid(T1) == ...)
...
else if (...)
...
else if (...)
...
}
}
class B
{
public:
void x()
{
Base *pSomePtr = ...;
pSomePtr->call(this,&B::y);
};
template <class T1,class T2>
void y()
{
// do stuff
};
};
Any help on how I could realize this would be greatly appreciated .
Thanks,
Stefan
This works for me, and it isn''t retricted to a known set of template
parameters. But it doesn''t pass the member function to be called to the
call method. I wasn''t sure if that was part of your requirements or not.
john
#include <iostream>
class B;
class Base
{
public:
virtual void call(B* b) = 0;
};
class B
{
public:
void x(Base *pSomePtr)
{
pSomePtr->call(this);
};
template <class T1,class T2>
void y()
{
std::cout << typeid(T1).name() << '' '' << typeid(T2).name() << ''\n'';
};
};
template <class T1, class T2>
class A : public Base
{
public:
void call(B* b)
{
b->y<T1, T2>();
}
};
int main()
{
A<int, doublea;
B b;
b.x(&a);
}
Thanks for your reply. Sorry I forgot to mention this, but
unfortunately it is one of the key requirements to specify the member
function to be called, so you can do somthing like this:
class B
{
public:
void x(Base *pSomePtr, Base *pSomeOtherPtr)
{
pSomePtr->call(this,&B::dosomething);
pSomePtr->call(this,&B::dosomethingelse);
pSomeOtherPtr->call(this,&B::doanotherthing);
};
template <class T1,class T2>
void dosomething()
{
std::cout << typeid(T1).name() << '' '' <<
typeid(T2).name() << ''\n'';
};
template <class T1,class T2>
void dosomethingelse()
{
std::cout << typeid(T1).name() << '' '' <<
typeid(T2).name() << ''\n'';
};
template <class T1,class T2>
void doanotherthing()
{
std::cout << typeid(T1).name() << '' '' <<
typeid(T2).name() << ''\n'';
};
};
If it wasn''t for that, I guess your solution would be ideal. The
problem with what I want is - of course - that it''s impossible using
member function pointers. So I''m looking for something that would
provide a similarly nice syntax.
--Stefan
这篇关于解析模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!