2014-11-20 30 views
5

我想const_value_type是const即使T是一個指針,與std::add_const<>
所以,我想這樣的事情是不可能的:如何添加常量預選賽模板

template<typename value_type, bool is_pointer> 
struct add_const_pointer{ 
    typedef const value_type type; 
}; 

template<typename value_type> 
struct add_const_pointer<value_type, true>{ 
    typedef const value_type *type; 
}; 

template<typename T> 
class Foo 
{ 
public: 
    typedef T value_type; 
    typedef add_const_pointer<std::remove_pointer<T>, std::is_pointer<T>::value>::type const_value_type; 
    // here I get compiler error: missing type specifier - int assumed. 
} 

,但我得到的編譯器錯誤:缺少類型說明符 - 假定爲int。

回答

5

clang錯誤信息將有助於

typedef typename add_const_pointer< 
//  ~~~~~~~~ Add typename 
        std::remove_pointer<T>, 
        std::is_pointer<T>::value>::type 
        const_value_type; 
+0

非常感謝您......現在我不知道爲什麼我要加類型名 – Quest 2014-11-20 17:21:16

+2

@Quest [這](http://stackoverflow.com/q/ 610245/1870232)將是有幫助的。 – P0W 2014-11-20 17:23:41