2016-08-06 9 views
3

<const char*>は次のコードでオプションですか?私は、g ++とclangがコンパイルされていても、それはうまくいきません。機能テンプレートの特殊化タイプ - これはオプションですか?

template<typename T> 
void debugRep2(T const& t) { 
    std::cout << "debugRep(const T& t)\n"; 
} 

template<> 
void debugRep2<const char*>(const char* const& t) { 
      //^^^^^^^^^^^^^ 
    std::cout << "const char*& t\n"; 
} 

int main() { 
    int n; 
    int *pn = &n; 
    debugRep2(n); 
    debugRep2(pn); 
} 

答えて

3

テンプレートの種類は、すでに関数のパラメータで指定されていて、そうです、この場合には、それはオプションであり、コンパイラ

template<> 
void debugRep2<const char*>(const char* const& t) { 
         // ^^^^^^^^^^^ already present 
    // ... 
} 

によって推定することができます。


実際にはその専門を書くための一般的な方法は、

template<> 
void debugRep2(const char* const& t) { 
    // ... 
} 
+0

別の質問になりますが、それはこのようtempalteを特化することが可能である: 'テンプレートは、<>無効debugRep2 (のconst int型&T) {} '? – user463035818

+0

@ tobi303私はそうは思わない、この場合はパラメータの型が矛盾する。 –

+0

テンプレート型が義務付けられている場合は分かりますか? – mike

関連する問題