如何从 C 中的方法类型推导类类型模板?
在如下所示的模板中,我希望调用 Run(&Base::foo)
成功,而无需两次命名 Base 类型(如编译 Run
调用)。我可以要那个吗?可能不需要添加大量 Boost 标头?
使用提供的代码,我收到错误:(
prog.cpp:26: error: no matching function for call to ‘Run(bool (Base::*)())’
您可以在 http://ideone.com/8NZkq< /a>):
#include <iostream>
class Base {
public:
bool foo() { return true; }
};
Base* x;
template<typename T>
struct Traits {
typedef bool (T::*BoolMethodPtr)();
};
template<typename T>
void Run(typename Traits<T>::BoolMethodPtr check) {
T* y = dynamic_cast<T*>(x);
std::cout << (y->*check)();
}
int main() {
Base y;
x = &y;
Run<Base>(&Base::foo);
Run(&Base::foo); // why error?
}
In templates as shown below, I would like the call Run(&Base::foo)
succeed without the need to name the Base type twice (as is done in the compiling Run<Base>(&Base::foo)
call). Can I have that? Possibly without adding a ton of Boost headers?
With the provided code, I get an error of:
prog.cpp:26: error: no matching function for call to ‘Run(bool (Base::*)())’
(you can fiddle with the snippet at http://ideone.com/8NZkq):
#include <iostream>
class Base {
public:
bool foo() { return true; }
};
Base* x;
template<typename T>
struct Traits {
typedef bool (T::*BoolMethodPtr)();
};
template<typename T>
void Run(typename Traits<T>::BoolMethodPtr check) {
T* y = dynamic_cast<T*>(x);
std::cout << (y->*check)();
}
int main() {
Base y;
x = &y;
Run<Base>(&Base::foo);
Run(&Base::foo); // why error?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Traits::BoolMethodPtr
中的T
处于非推导上下文中,因此编译器不会从调用中自动推导 T 的类型。这是因为可能有这样的代码:
如果您可以不使用
Traits
类,则可以将Run
编写为:在此上下文中,
Class
可以推断为Base
的意思The
T
inTraits<T>::BoolMethodPtr
is in a non-deduced context, so the compiler will not deduce automatically from the call what type T should be.This is because there could be code like this:
If you can do without the
Traits<T>
class, you can writeRun
as:In this context,
Class
can be deduced to meanBase
要区分一种类型(任何类型),请使用部分特化。没有函数模板部分专业化,因此您需要直接在其参数类型上参数化函数并检索内部的类类型。
To pick apart a type, any type, use partial specialization. There is no function template partial specialization, so you'll need to directly parameterize the function on its argument type and retrieve the class type inside.
我认为这是一个非推导的上下文。
我认为这句话适用于这种情况。但是一些模板大神需要批准我的理解。
I think this is a non deduced context.
I think this is the quote that applies in this case. But some template gods need to ratify my understanding.
当编译器尝试匹配模板参数时,它仅考虑主类类型。换句话说,当它遇到表达式:
...并且它试图找出
Run
的模板参数时,它只考虑foo
本身的类型,而不会不考虑foo
所属的任何类。编辑:
foo
的类型是bool(Base::*)(void)
,但您希望编译器找到的只是Base
>When the compiler tries to match a template argument, it only considers the primary class type. In other words, when it encounters the expression:
...and it's trying to figure out the template parameter for
Run
, it only considers the type offoo
itself, and doesn't consider whatever classfoo
is a part of.EDIT:
And the type of
foo
isbool(Base::*)(void)
, but what you want the compiler to find is justBase