Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #111

Re: static_assert in constexpr functions

From Daniel Krügler<daniel.kruegler@googlemail.com>
Newsgroups comp.std.c++
Subject Re: static_assert in constexpr functions
Date 2011-04-18 13:05 -0600
Organization A noiseless patient Spider
Message-ID <iogkut$d90$1@dont-email.me> (permalink)
References <b2fb8f20-9343-40b6-86b2-e4364ad163b3@z31g2000vbs.googlegroups.com>

Show all headers | View raw


On 2011-04-17 09:33, Andrzej Krzemieński wrote:
>
>  Hi,
>  In the latest C++ Standard Draft (N3291) static_assert declarations
>  are allowed in constexpr functions. I do not understand how they work.
>  Consider:
>
>    constexpr unsigned half( int i )
>    {
>      static_assert( i % 2 == 0, "requires even arg" );
>      return i / 2;
>    }
>
>  static_assert requires a constant expression. Is "i % 2 == 0" a
>  constant expression or not? My intuitive understanding is that it is
>  not known at the time of function declaration if 'i' refers to
>  'constexpr' object or not. But if I try to read the standard very
>  formally, it looks that (as per 5.19/2), since 'i' is not "a glvalue
>  of integral or enumeration type that refers to a non-volatile const
>  object with a preceding initialization, initialized with a constant
>  expression," static_assert declaration is incorrect.
>
>  Is it correct interpretation that we cannot use function arguments in
>  static_assert declarations for constexpr functions?

Bo Persson already provided the correct answer to this question.

Just as an example consider that you want to make std::forward a
constexpr function (which would be *extremely* useful), you could
realize the specification by providing this overload set in namespace std:

template<class T>
constexpr T&&
forward(typename remove_reference<T>::type&  t)
{
   return static_cast<T&&>(t);
}

template<class T>
constexpr T&&
forward(typename remove_reference<T>::type&&  t)
{
   static_assert(!is_lvalue_reference<T>::value,
     "T must not be an lvalue-reference"
   );
   return static_cast<T&&>(t);
}

It was possible before as well via one further indirection, but that
indirection usually leads to worse error messages on typical
implementations because of the call stack depth involved.

HTH&  Greetings from Bremen,

Daniel Krügler


-- 
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

static_assert in constexpr functions Andrzej Krzemieński <akrzemi1@gmail.com> - 2011-04-17 01:33 -0600
  Re: static_assert in constexpr functions "Bo Persson" <bop@gmb.dk> - 2011-04-17 09:42 -0600
  Re: static_assert in constexpr functions Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-04-18 13:05 -0600

csiph-web