Boost logo

Boost :

Subject: Re: [boost] [utility] Proposal to extract some components from Boost.Log
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2013-09-01 07:46:40


On 2013-09-01 07:36, Andrey Semashev wrote:
> On Sunday 01 September 2013 11:06:16 Daryle Walker wrote:
>
>> I don’t think you need a “constexpr_if.” You can unconditionally put
>> BOOST_CONSTEXPR in the definition. This will unconditionally add
>> “constexpr” to the member operator (for C++11 systems), which will be
>> ignored if the compiler determines that the attached function (or a
>> particular call) is disqualified from being used in constant expressions.
>> So any extra preprocessor work, or a variant macro, is unnecessary.
>
> No, that doesn't work.
>
> struct foo
> {
> void* m_p;
>
> constexpr explicit operator bool () const
> {
> return !this->operator!();
> }
>
> bool operator! () const
> {
> return !m_p;
> }
> };
>
> g++ -std=gnu++11 constexpr_operator.cpp -o constexpr_operator
> constexpr_operator.cpp: In member function ‘constexpr foo::operator bool()
> const’:
> constexpr_operator.cpp:9:27: error: call to non-constexpr function ‘bool
> foo::operator!() const’

But this works:

struct foo
{
  void* m_p;

  constexpr explicit operator bool () const
  {
    return !this->operator!();
  }

  constexpr bool operator! () const
  {
    return !m_p;
  }
};

int main()
{
  constexpr auto f = foo{nullptr};
  constexpr auto b = bool(f);
  return b;
}

John Bytheway


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