auto
基本的に以下で、キーワードを使用しなければならないことを回避できるかどうかを確認したい
次のコードがあるとします [g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) & clang バージョン 3.6.0 で動作] :
//g++ -std=c++14 test.cpp
//test.cpp
#include <iostream>
using namespace std;
template<typename T>
constexpr auto create() {
class test {
public:
int i;
virtual int get(){
return 123;
}
} r;
return r;
}
auto v = create<int>();
int main(void){
cout<<v.get()<<endl;
}
宣言/定義の時点でキーワードをv
使用するのではなく
、どのようにタイプを指定できますか? auto
試しcreate<int>::test v = create<int>();
ましたが、これはうまくいきません。
ps
1)これは、 Returning a class from a constexpr function requires virtual keyword with g++ even through the code is the sameで私が尋ねていた質問とは異なります。
2)関数の外でクラスを定義したくありません。