小编use*_*989的帖子

使用boost :: hana获取函数参数的类型

我知道如何以旧的方式获取函数的参数类型,但我想知道是否有一个很好的新方法与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这里?

c++ boost c++14 boost-hana

3
推荐指数
1
解决办法
253
查看次数

如何将一个模板类转换为另一个模板类?

这个问题最好用例子说明

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)

c++

3
推荐指数
1
解决办法
84
查看次数

检查类是否具有签名功能

此站点上有其他答案使用SFINAE但非C++ 11代码,还有其他人使用C++ 11代码(如decltypes)来简化此过程.但是,我不确定如何检查类是否具有特定签名的函数.

我想检查一个类是否具有函数receive(const Event &)where Event在调用check函数时指定的类类型.

c++ sfinae c++11

2
推荐指数
1
解决办法
2441
查看次数

使用模板化基类型对派生结构进行struct初始化

我试图在派生自模板结构上使用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返回trueMoveEvent.那么这里的问题是什么?为什么我不能使用struct初始化?

c++ c++11

0
推荐指数
1
解决办法
902
查看次数

标签 统计

c++ ×4

c++11 ×2

boost ×1

boost-hana ×1

c++14 ×1

sfinae ×1