小编vij*_*vij的帖子

模板调用:未调用实际专业化

#include <iostream>

using namespace std;

template<typename T>
void test() {
   cout << "1";
}

template<>
void test<std::string>() {
   cout << "2";
}

int main() {
   test<std::string()>(); //expected output 2 but actual output 1
}
Run Code Online (Sandbox Code Playgroud)

为什么输出1而不是2?

c++ string templates

5
推荐指数
1
解决办法
103
查看次数

typedef with templates

template<class T> struct A {
    typedef int Int;
    A::Int b; // Line 1 (fails)
    Int c; // Line 2 (compiles)
};

int main(){    
   A<int> x;
   x.c = 13;
}
Run Code Online (Sandbox Code Playgroud)

错误

error: ISO C++ forbids declaration of ‘Int’ with no type
error: extra qualification ‘A<T>::’ on member ‘Int’
error: expected ‘;’ before ‘b’
Run Code Online (Sandbox Code Playgroud)

第1行失败但第2行编译.为什么?

c++ templates typedef

4
推荐指数
1
解决办法
210
查看次数

标签 统计

c++ ×2

templates ×2

string ×1

typedef ×1