2015-05-28 121 views
3

如果我想要一些類型是專用於它的模板參數我一般使用結構:你可以專門使用語句嗎?

template <bool value> 
struct IsTrue; 

template <> 
struct IsTrue<true> : std::true_type {}; 

template <> 
struct IsTrue<false> : std::false_type {}; 

一個空的結構是獲得其從繼承唯一的功能是不是真的那麼從using聲明不同,所以我想知道,using陳述中是否存在類似於模板專門化的內容?下面我想要的僞代碼:

template <bool value> 
using IsTrue; 

template <> 
using IsTrue<true> = std::true_type; 

template <> 
using IsTrue<false> = std::false_type; 

是這樣的可能嗎?它會被稱爲什麼?

+0

我不知道它是否可能,但是如果它使用'using'語句就像'typedef'那樣創建* type別名*。 –

+0

查看類型別名http://en.cppreference.com/w/cpp/language/type_alias –

+3

不,別名模板不能部分或明確專用。這是一件好事,因爲否則模板參數推導不能查看它。 –

回答

3

不,別名模板不能是部分或明確專用的。

An earlier design確實允許專業化,但由此產生的語義相當...奇怪,至少從今天的角度來看。例如,在這樣的設計,下面的程序將聲明兩個不同函數模板:

template<class, class> class Meow {}; 
template<class T> using MeowInt = Meow<int, T>; 
template<class> void f(Meow<int, T>); 
template<class> void f(MeowInt<T>); 

這個調用將無法編譯,因爲你不能就能夠推斷出模板參數:

template<class T> using Purr = T; 
template<class T> void f(Purr<T>); 

f(42); 
相關問題