2016-07-19 3 views
0

struct hash<template class Key>のテンプレート構文に慣れていますが、使用するときの違いは何ですか? ?空の括弧と構造体を使用したテンプレートの特殊化

namespace std { 

    template <> 
    struct hash<Key> 
    { 
    std::size_t operator()(const Key& k) const 
    { 
     .... 
    } 
    }; 
} 

それが道だと私はtemplate <>の意味を検索しなかったと私は(私は願って)理解していることに注意してください、struct<Key> Iの使用と一緒に非マッチしたケースを指定するには、パターンマッチングを使用して、しかしときそれの動機を理解していない。

+0

これはテンプレートの特殊化の構文です。 – melpomene

+0

@melpomene構造体/クラスの文脈では? Template関数を使用している場合は、テンプレートを使用して特殊化しないでください:template void foo(){} – user695652

+1

@ user695652これは特殊化ではなく、通常のテンプレート宣言です。 – 0x5453

答えて

6

テンプレートの特殊化の異なるレベルがあります

1)テンプレート宣言(NO専門)

template <class Key, class Value> 
struct Foo {}; 

2)部分的な特殊

template <class Key> 
struct Foo<Key, int> {}; 

3)フル/明示的な特殊

template <> 
struct Foo<std::string, int> {}; 

テンプレートをインスタンス化する際に続いて、コンパイラが利用できる最も特別な定義を選択します:テンプレート関数のための

Foo<std::string, std::string> f1; // Should match #1 
Foo<int,   int>   f2; // Should match #2 
Foo<std::string, int>   f3; // Should match #3 

#1と#3の作業を同様。

関連する問題