How to force a compile failure if a template parameter isn't floating-point
I have some functions that I want to ensure (with a compile failure - because it means a logical mistake) that they are not instantiated if the template parameter is not floating point type. In effect, #error Type Not floating point! 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. I can't find BOOST_CONCEPT_ASSERT((boost::FloatingPoint<FPT>)); or boost::function_requires< boost::Integer<FPT> >(); Suggestions welcome :-) Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com
If you need only floating points only, then what do you need template for? Make two overloaded versions of your routine: std::string f(float v) { ... } std::string f(double v) { ... } That's it :) -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Paul A. Bristow Sent: Wednesday, July 18, 2012 1:54 PM To: boost-users@lists.boost.org Subject: [Boost-users] How to force a compile failure if a template parameter isn't floating-point I have some functions that I want to ensure (with a compile failure - because it means a logical mistake) that they are not instantiated if the template parameter is not floating point type. In effect, #error Type Not floating point! 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. I can't find BOOST_CONCEPT_ASSERT((boost::FloatingPoint<FPT>)); or boost::function_requires< boost::Integer<FPT> >(); Suggestions welcome :-) Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 07/18/2012 12:54 PM, Paul A. Bristow wrote:
Suggestions welcome :-)
Hi,
Either:
http://www.boost.org/doc/libs/1_47_0/doc/html/boost_staticassert.html
BOOST_STATIC_ASSERT(boost::is_floating_point<FPT>::value);
Or:
http://www.boost.org/doc/libs/1_47_0/libs/utility/enable_if.html
template <typename FPT>
std::string f(FPT v,
typename
boost::enable_if_c
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Maxime van Noppen Sent: Wednesday, July 18, 2012 12:03 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] How to force a compile failure if a template parameter isn't floating-point
On 07/18/2012 12:54 PM, Paul A. Bristow wrote:
Suggestions welcome :-) http://www.boost.org/doc/libs/1_47_0/doc/html/boost_staticassert.html
BOOST_STATIC_ASSERT(boost::is_floating_point<FPT>::value);
So why didn't I think of that ;-) (Even when I had used BOOST_STATIC_ASSERT() elsewhere in the program!) Thanks Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com
On 7/18/2012 4:02 AM, Maxime van Noppen wrote:
But you might as well consider having:
template <typename FPT> std::string f_impl(FPT v) { }
std::string f(float v) { return f_impl(v); } std::string f(double v) { return f_impl(v); }
Don't forget long double. But +1 to the simple suggestion. -- Eric Niebler BoostPro Computing http://www.boostpro.com
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Eric Niebler Sent: Wednesday, July 18, 2012 5:44 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] How to force a compile failure if a template parameter isn't floating-point
On 7/18/2012 4:02 AM, Maxime van Noppen wrote:
But you might as well consider having:
template <typename FPT> std::string f_impl(FPT v) { }
std::string f(float v) { return f_impl(v); } std::string f(double v) { return f_impl(v); }
Don't forget long double. But +1 to the simple suggestion.
Ok - I'm all for KISS - BUT the imposing spectre of multiprecision looms ;-) But thanks for the helpful suggestions. Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com
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
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of John Maddock Sent: Wednesday, July 18, 2012 3:41 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] How to force a compile failure if a templateparameter isn't floating-point
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
::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.
Well - out of curiosity (having solved my problem neatly with BOOST_STATIC_ASSERT) I had a brief skirmish with this but got error C4519: default template arguments are only allowed on a class template and my need is for a free function, not a member function. It isn't worth spending time on me or you making this work - having solved my problem (which was actually downstream and these checks are now redundant). However, in reading the docs for enable_if (and to some extent type_traits), I felt they were not up to the current best examples of documentation. They lack enough tutorial (especially at an introductory level) material, and especially the invaluable fully worked (and commented) examples. I think we need these improved if these undoubtedly invaluable tools are to come out of the 'gurus and brainiacs only' zone and be suitable for 'bears of little brain' ;-) Might this be a GSoC project? Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com
participants (5)
-
Alexander Stoyan
-
Eric Niebler
-
John Maddock
-
Maxime van Noppen
-
Paul A. Bristow