小编Tre*_*ife的帖子

在 C++ 中键入作为返回类型

是否有可能将类型作为函数的返回类型返回,并将其用于成员变量,使用以下内容:

constexpr type myFunction(int a, int b){
     if(a + b == 8) return int_8t;
     if(a + b == 16) return int_16t;
     if(a + b == 32) return int_32t;
     return int_64t;
}

template<int x, int y>
class test{
    using type = typename myFunction(x, y);
    private:
    type m_variable;
};
Run Code Online (Sandbox Code Playgroud)

在 Qt 中尝试此示例时,它说

Error: 'constexpr' does not name a type
Error: 'test' is not a template type
class test{
      ^
Run Code Online (Sandbox Code Playgroud)

在之前的一个问题中,有人向我展示了http://en.cppreference.com/w/cpp/types/conditional这个函数,但它只适用于 2 种类型。

c++ templates

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

标签 统计

c++ ×1

templates ×1