Returning a class from a constexpr function requires virtual keyword with g++

。_饼干妹妹 提交于 2019-12-06 13:26:53

Classes defined inside a function cannot be accessed outside the function. My suggestion are: declare test outside the function and add const qualifier to get function.

#include <iostream>
using namespace std;

  class test {
  public:
    int i;
    int get() const {
      return 123;
    }
  };

template<typename T>
constexpr test create() {
  return test();
}

auto v = create<int>();

int main(void){
  cout<<v.get()<<endl;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!