Set the default operator >> to not compile unless there is a scalar conversion, so it doesn't interfere with user-defined types

This commit is contained in:
beder
2011-11-01 17:19:03 -05:00
parent a853a7a14d
commit 50b6a02907
3 changed files with 13 additions and 2 deletions

View File

@@ -14,6 +14,17 @@
namespace YAML namespace YAML
{ {
// traits for conversion
template<typename T>
struct is_scalar_convertible { enum { value = is_numeric<T>::value }; };
template<> struct is_scalar_convertible<std::string> { enum { value = true }; };
template<> struct is_scalar_convertible<bool> { enum { value = true }; };
template<> struct is_scalar_convertible<_Null> { enum { value = true }; };
// actual conversion
inline bool Convert(const std::string& input, std::string& output) { inline bool Convert(const std::string& input, std::string& output) {
output = input; output = input;
return true; return true;

View File

@@ -66,7 +66,7 @@ namespace YAML
const T to() const; const T to() const;
template <typename T> template <typename T>
friend YAML_CPP_API void operator >> (const Node& node, T& value); friend YAML_CPP_API typename enable_if<is_scalar_convertible<T> >::type operator >> (const Node& node, T& value);
// retrieval for maps and sequences // retrieval for maps and sequences
template <typename T> template <typename T>

View File

@@ -20,7 +20,7 @@ namespace YAML
} }
template <typename T> template <typename T>
inline void operator >> (const Node& node, T& value) { inline typename enable_if<is_scalar_convertible<T> >::type operator >> (const Node& node, T& value) {
if(!ConvertScalar(node, value)) if(!ConvertScalar(node, value))
throw InvalidScalar(node.m_mark); throw InvalidScalar(node.m_mark);
} }