我知道如何以旧的方式获取函数的参数类型,但我想知道是否有一个很好的新方法与Hana一起做?例如,我想要这样的东西:
struct foo {
int func(float);
};
auto getFuncType(auto t) -> declval<decltype(t)::type>()::func(TYPE?) {}
getFunType(type_c<foo>); // should equal type_c<float> or similar
Run Code Online (Sandbox Code Playgroud)
我如何到达TYPE这里?
这个问题最好用例子说明
template <typename T>
struct expression {
};
template <typename T>
struct variable {
operator expression<T>() const {
return {};
}
};
template <typename T>
struct member_variable {
template <typename U>
void operator=(const expression<U>&) {}
};
int main() {
variable<int> a;
member_variable<float> b;
b=a;
}
Run Code Online (Sandbox Code Playgroud)
就目前而言,不能使用赋值运算符,因为推导存在问题U(至少我认为这是错误告诉我的).如何编译代码?我也尝试过为expression这种方法创建一个转换构造函数variable,但也没有用.我想避免继承expression,因为它在实践中比其他两个更重.
该operator=*是其他高级用法,如添加的立场operator*(expression<T>, expression<U>),并能与调用它们a*b.
我尝试过Clang trunk(8.0.0)和GCC trunk(9.0.0)-std=c++17,以及MSVC 15.9.3.
Clang消息:
prog.cc:28:6: error: no viable overloaded '='
b=a;
~^~
prog.cc:20:8: note: candidate …Run Code Online (Sandbox Code Playgroud) 此站点上有其他答案使用SFINAE但非C++ 11代码,还有其他人使用C++ 11代码(如decltypes)来简化此过程.但是,我不确定如何检查类是否具有特定签名的函数.
我想检查一个类是否具有函数receive(const Event &)where Event在调用check函数时指定的类类型.
我试图在派生自模板结构上使用struct初始化.代码如下:
template <class Derived>
struct Event{
//the level of access on the ctor has nothing to do with the problem
//protected:
// Event() = default;
};
struct MoveEvent: Event<MoveEvent>{
int x, y;
};
int main(){
//how do I make this work?
//MoveEvent event = {.x =5, .y = 4};
}
Run Code Online (Sandbox Code Playgroud)
我认为这可能是与CTRP,但改变Event<MoveEvent>来Event<int>产生同样的问题.此外,我认为这是与POD的问题,而是std::is_pod返回true了MoveEvent.那么这里的问题是什么?为什么我不能使用struct初始化?