Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-01-09 12:52:26


From: "Derek Ross" <dross_at_[hidden]>
> Hello,
>
> I'd like to convert this run-time assertion to a compile time MPL check:
>
> template <class T>
> bool SetOption(COptionInfo& o, const char* key, const T& value)
> {
> assert( typeid(T)==typeid(bool)
> || typeid(T)==typeid(int)
> || typeid(T)==typeid(double)
> || typeid(T)==typeid(string));
> ...etc...
>
> As you can probably guess, this is a template function that should only
> be called with types of int, bool, std::string or double.

This would make a nice "MPL Hello World" example.

Here is one non-MPL low tech old fashioned C++ solution.

inline void assertSetOption(bool const *) {}
inline void assertSetOption(int const *) {}
inline void assertSetOption(double const *) {}
inline void assertSetOption(string const *) {}

template <class T>
bool SetOption(COptionInfo& o, const char* key, const T& value)
{
    assertSetOption(&value);
// ...
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk