#include <iostream>
#include <string>
using namespace std;
template <typename T,
typename T::type N,
typename T::strata& X>
struct SomeClass{};
struct S1
{
typedef int type;
typedef string strata;
};
int main() {
SomeClass<S1, 3, string("erg")> x;
}
失敗消息:非類型模板參數沒有引用任何聲明?
g++ templ.cc -o templ -std=c++14 templ.cc:18:20: error: non-type template argument does not refer to any declaration SomeClass<S1, 3, string("erg")> x; ^~~~~~~~~~~~~ templ.cc:8:24: note: template parameter is declared here typename T::strata& X>
爲什麼它的工作原理爲INT而不是字符串? 爲什麼它說該字符串是非類型的參數?
因爲它不是一個類型。非類型參數僅限於基元(實際上,積分和指針) –
@PasserBy也可以使用此處所用的類類型引用。 – aschepler
而且...我沒有看到那個參考:) –