2013-05-22 1 views
0

Я пытаюсь получить эту работу:шаблонный функция, которая принимает только строку или арифметику

template<class Type> 
typename boost::enable_if< boost::mpl::or_< 
boost::is_arithmetic<Type>, 
is_string<Type> > >::type 
get(const std::string &argPath, const Type &argDefault) { 
    bool caught = false; 
    std::stringstream ss; 
    Type value; 

    try { 
     value = ptree_.get<Type>(argPath); 
    } catch(ptree_bad_path &e) { 
     caught = true; 
    } 

    if(caught) 
     value = argDefault; 

    ss << value; 
    parameters_.insert(std::pair<std::string, std::string>(argPath, ss.str())); 
    return value; 
} 

Я использовал следующий IS_STRING тип признака: Type trait for strings

Моя цель ограничить мою Type нанизывать или арифметический тип, чтобы я мог нажать его на мой stringstream.

Так что это строит, но когда я пытаюсь использовать его, он возвращает следующие ошибки:

error: void value not ignored as it ought to be

In member function ‘typename boost::enable_if, is_string, mpl_::bool_, mpl_::bool_, mpl_::bool_ >, void>::type FooClass::get(const std::string&, const Type&) [with Type = uint8_t]’

error: return-statement with a value, in function returning 'void'

Вот как я пытаюсь использовать:

FooClass f; 
item_value = f.get("tag1.tag2.item", DEFAULT_ITEM_VALUE); 

Любая помощь приветствуется, заранее спасибо!

ответ

4

От http://www.boost.org/doc/libs/1_53_0/libs/utility/enable_if.html, enable_if имеет второй параметр, который по умолчанию опорожнить:

template <bool B, class T = void> 
struct enable_if_c { 
    typedef T type; 
}; 

мне кажется, что вам нужно включить тип возвращаемого в вашем enable_if. (Он по умолчанию не действует.)

template<class Type> 
typename boost::enable_if< boost::mpl::or_< 
    boost::is_arithmetic<Type>, 
    is_string<Type> >, 
Type >::type 
get(const std::string &argPath, const Type &argDefault); 
+0

Вы правы, теперь он работает так, как ожидалось! Большое спасибо, вы сделали мой день! – Syffys

 Смежные вопросы

  • Нет связанных вопросов^_^