Boost logo

Boost Users :

Subject: Re: [Boost-users] How to force a compile failure if a templateparameter isn't floating-point
From: John Maddock (boost.regex_at_[hidden])
Date: 2012-07-18 10:40:51


> template <typename FPT>
> std::string f(FPT v)
> {
> ...
> }
>
> At present I have added a run-time check, but doesn't prevent compilation
> (and instantiation).
>
> if (boost::is_floating_point<FPT>::value; == false)
> {
> assert fail or something and/or
> return " "Type Not floating point!";
> }
>
> I don't see how enable_if can help either.

template <typename FPT>
typename enable_if<is_floating_Point<FPT>, std::string>::type f(FPT v)
{
...
}

If FTP is not a built in floating point type, then no overload for f() will
be found and you'll get a compiler error.

HTH, John.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net