Boost logo

Boost :

Subject: Re: [boost] [config] consexpr workaround
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-11-22 09:09:26


vicente.botet wrote:
> From: "Stewart, Robert" <Robert.Stewart_at_[hidden]>
>
> > The purpose of constexpr is to mark something as a compile
> > time constant such that it can be used in other compile time
> > expressions, including types that weren't allowed as compile
> > time constants before. It allows for constant-expression
> > functions, data, and even constructors. Constant-expression
> > data disallows dynamic initialization which const objects can
> > undergo. Without constexpr support, neither
> > constant-expression functions nor constructors are supported,
> > nor is floating point constant-expression data. Thus, const
> > is inappropriate in those cases, so BOOST_CONSTEXPR_OR_CONST
> > cannot be used.
>
> I think that we need to declare a constant variable as
> constexpr if we want it to be part of other constexpr. Am I wrong?

You're partly right. Compile time constant integral values can be expressed several ways and all can be used in a constexpr. If you need a constexpr for another purpose, then the other approaches don't apply. However, in that case, BOOST_CONSTEXPR_OR_CONST doesn't apply. If that macro does apply, then you don't need constexpr.

> > For non-floating point constant-expression data, const
> > could work fairly well, but it permits dynamic
> > initialization, so for platforms supporting constexpr, code
> > would fail to compile that mistakenly compiled on platforms
> > without constexpr support.
>
> You are right. But it works well when the the data is
> staticaly initialized, which is the case for most of the constants.

Sure, but you don't need constexpr in that case, so you don't need BOOST_CONSTEXPR_OR_CONST.

> > I don't think BOOST_CONSTEXPR_OR_CONST is helpful.
>
> Do you mean that a library that must provide a C++03
> interface can not use constexpr on C++0x compilers as the
> meaning can change? In Boost.Ratio "num" and "den" are
> defined as const for C++03 compilers, and need to be
> constexpr for C++0x compilers. How do you write such a code
> in a "portable" way?

They don't need to be constexpr. The C++03 code will work.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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