2012-10-11 20 views
0

我在使用boost_type_traits的代碼中遇到問題。 這是代碼的一個相當複雜的一部分,但我可以隔離,讓編譯錯誤的一部分:在boost :: type_traits ::條件下編譯類型特徵

template<const size_t maxLen> 
class MyString { 
public: 
    typedef boost::conditional<(maxLen > 0), char[maxLen+1], std::string> ObjExternal; 
}; 

template <class T> 
class APIBase { 
public: 
    typedef T obj_type; 
    typedef typename T::ObjExternal return_type; 
}; 

template <class T> 
int edit(const T& field, const typename T::return_type& value) 
{ 
    return 0; 
} 

int myFunction() 
{ 
    APIBase<MyString<10> > b; 
    char c[11]; 
    return edit(b, c); 
} 

這提供了以下錯誤:

TEST.CPP:在函數「廉政myFunction的( )': tes.cpp:109:error:調用'編輯(APIBase> &,char [11])' tes.cpp:100:沒有匹配函數:備註:候選值爲:int edit(const T &, const typename T :: return_type &)[with T = APIBase>]

不過,如果我通過

MyString<10>::ObjExternal c; 

更改線路的代碼

char c[11]; 

它的工作原理。同樣,如果不是我的

typedef char ObjExternal[maxLen+1]; 

更改線路

typedef boost::conditional<(maxLen > 0), char[maxLen+1], std::string> ObjExternal; 

它也適用。我認爲這是boost :: conditional的一個問題,因爲它似乎沒有被正確評估。在我的代碼中是否有問題,或者有替代方法可以用來代替boost :: conditional來實現這個功能?

我想使用第二個選項,但我不能使用MAXLEN爲0

回答

1

您需要使用的typedef type成員由conditional提供,而不是條件類型本身。

變化:

typedef boost::conditional<(maxLen > 0), 
          char[maxLen+1], 
          std::string> ObjExternal; 

到:

typedef typename boost::conditional<(maxLen > 0), 
            char[maxLen+1], 
            std::string>::type ObjExternal;