テンプレートと構造体を使用しようとしましたが、機能しません。いろいろ調べましたが、解決策が見つかりません。
#include <iostream>
using namespace std;
template<struct S>
int add(S s) {
return s.num + s.num2;
}
int main() {
struct {
int num = 10;
int num2 = 20;
} test;
cout << add(test) << endl;
return 0;
}
gcc のエラーは次のとおりです。
test.cpp:5:17: error: ‘struct S’ is not a valid type for a template non-type parameter
test.cpp: In function ‘int add(S)’:
test.cpp:6:5: error: ‘s’ has incomplete type
test.cpp:5:17: error: forward declaration of ‘struct S’
test.cpp: In function ‘int main()’:
test.cpp:13:19: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
test.cpp:14:20: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
test.cpp:17:21: error: no matching function for call to ‘add(main()::<anonymous struct>&)’
test.cpp:17:21: note: candidate is:
test.cpp:6:5: note: template<<declaration error> > int add(S)
test.cpp:6:5: note: template argument deduction/substitution failed:
test.cpp:17:21: note: cannot convert ‘test’ (type ‘main()::<anonymous struct>’) to type ‘S’