Boost logo

Boost :

Subject: Re: [boost] [c++11] compile_if utility
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-07-04 13:23:32

> > Hello all,
> >
> > I was thinking how to program assertion requirements in C++11. I need
> > to program something like this
> >
> > if "has_equal_to<T>" then "assert(back() == value)"
> > else do nothing, don't even compile "assert(back() == value)"
> >
> > Using C++11 lambdas, I was able to program a compile_if utility to do
> > the following:
> >
> > compile_if< boost::has_equal_to<T> >([&]() {
> > assert(this->back() == value); // compiled and executed iff
> > has_equal_to<T>
> > });
> >
> > Any interest in adding compile_if to Boost.Utility? It could be
> > renamed exec_if if compile_if is too "strong".
> >
> > I didn't play with compile_if much so there might cases where it
> > doesn't work... but I wanted to share it with the ML anyway:
> Yep, I spoke too soon :( The code compiles on G++ 4.5.3 -std=c++0x but
> it does not compile on MSVC10 which complains x has no operator==...
> who's right? (I'm afraid MVSC...).
> If so, is there another way to do this in C++11?

If C++11 had local class templates, then you could write a macro that
emits the declaration of a local class template with a bool template
parameter with a static function that performs the assert (for the true
specialization) or does nothing (for the false specialization),
and then call it with the condition as the template parameter.

Are there any compilers that support local class templates and/or local
classes with template members as an extension? I have repeatedly found
myself wishing for them, and they seem like a logical next step after
adding support for local classes as template parameters in C++11.


Boost list run by bdawes at, gregod at, cpdaniel at, john at