是否有可能将类型作为函数的返回类型返回,并将其用于成员变量,使用以下内容:
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 种类型。