Hello, I have implemented the following template: template<typename Value> struct Validator { typedef bool result_type; bool operator()(const std::wstring& newVal)const { boost::lexical_cast<typename Value>(newVal); return true; } }; now here is the problem that for pointers the lexical cast doesn't work. I have now made the following declaration: boost::lexical_cast<boost::mpl::if_< boost::is_pointer<typename Value>, boost::remove_pointer<typename Value>::type, typename Value>>::type>(newVal); It works now for pointers too, but now I have the problem with the char type. First I have made a specialization for this type. Is it here also possible to make only one specialization or have I to provide both for pointer and value? Is there another way to solve that problem? best regards Hansjörg
Hansi <hansipet <at> web.de> writes:
It works now for pointers too, but now I have the problem with the char type. First I have made a specialization for this type. Is it here also possible to make only one specialization or have I to provide both for pointer and value?
I wonder what your Validator validates? You seem to be mixing values and pointers which is not a good idea. BTW, http://www.boost.org/doc/libs/1_35_0/libs/conversion/lexical_cast.htm#change... "The previous version of lexical_cast allowed unsafe and meaningless conversions to pointers. The current version now throws a bad_lexical_cast for conversions to pointers: lexical_cast<char *>("Goodbye, World") now throws an exception instead of causing undefined behavior." -- Alexander
Alexander Nasonov schrieb:
Hansi <hansipet <at> web.de> writes:
It works now for pointers too, but now I have the problem with the char type. First I have made a specialization for this type. Is it here also possible to make only one specialization or have I to provide both for pointer and value?
I wonder what your Validator validates? You seem to be mixing values and pointers which is not a good idea.
At the moment I mix references and pointers. I know that it would be better to not use pointers. But there are cases where it is easier to use it. In this cases it is allways != NULL There are made a few more checks with the validator (ranges, possible values e.g.).
BTW,
http://www.boost.org/doc/libs/1_35_0/libs/conversion/lexical_cast.htm#change...
"The previous version of lexical_cast allowed unsafe and meaningless conversions to pointers. The current version now throws a bad_lexical_cast for conversions to pointers: lexical_cast<char *>("Goodbye, World") now throws an exception instead of causing undefined behavior."
-- Alexander
Hansi <hansipet <at> web.de> writes:
At the moment I mix references and pointers. I know that it would be better to not use pointers. But there are cases where it is easier to use it. In this cases it is allways != NULL There are made a few more checks with the validator (ranges, possible values e.g.).
This is probably more scalable (you may need disable_if, though): template<class T> T& unwrap(T&); template<class T> T& unwrap(T*); char const* unwrap(char const*); template<class T> T& unwrap(reference_wrapper<T>); template<class T> T& unwrap(optional<T>); -- Alexander
Hansi wrote:
I have now made the following declaration:
boost::lexical_cast<boost::mpl::if_< boost::is_pointer<typename Value>, boost::remove_pointer<typename Value>::type, typename Value>>::type>(newVal);
It works now for pointers too, but now I have the problem with the char type. First I have made a specialization for this type. Is it here also possible to make only one specialization or have I to provide both for pointer and value?
There is a boost::is_same that you could use for comparing T to char* and/or char const*. I don't think you'll get the behavior you are after, but it will let you do what you've set out to do.
participants (3)
-
Alexander Nasonov
-
Hansi
-
Noah Roberts