Bug 67333 - [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types
Summary: [C++11][constexpr] constexpr functions incorrectly prohibit taking references...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2015-08-24 04:39 UTC by Melissa
Modified: 2016-10-12 20:11 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 6.0
Last reconfirmed: 2015-08-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Melissa 2015-08-24 04:39:25 UTC
GCC 4.7.3 (at least) through GCC 6.0 reject the following due to "meow has side-effects":

#include <cstddef>
#include <type_traits>

template <typename T, std::size_t S>
constexpr std::size_t lengthof(const volatile T (&)[S])
{
    return S;
}

int main()
{
    volatile int meow[4];
    static_cast<void>(meow); // shut up warning
    return static_cast<int>(std::integral_constant<std::size_t,
        lengthof(meow)>::value);
}

I believe that this is legal per [expr.const] in the Standard, because the volatile parameter is never used in an lvalue-to-rvalue conversion, which is what [expr.const] disallows in constant-expressions for volatile types.  Clang versions that understand this code accept it; Visual C++ 2015 does as well.

Someone who replied to my question on the "std-discussion" mailing list suggested that this is also technically legal as well:

#include <type_traits>

constexpr int Test(int x)
{
    volatile int v = x;
    return x;
}

int main()
{
    return std::integral_constant<int, Test(2)>::value;
}

GCC also rejects this, but Clang accepts this as well.  Any attempt to read v will fail, though, so Clang is enforcing the rule.  I'm not on my Windows machine as I write this, so I can't check MSVC.
Comment 1 Mikhail Maltsev 2015-08-24 05:50:28 UTC
EDG accepts the first testcase. The second one is rejected, but presumably due to incomplete C++14 support.

In potential_constant_expression_1 we have a check:

  if (TREE_THIS_VOLATILE (t))
    {
      if (flags & tf_error)
        error ("expression %qE has side-effects", t);

The comment in tree.h says:
/* Nonzero means this expression is volatile in the C sense:
   its address should be of type `volatile WHATEVER *'.
   In other words, the declared item is volatile qualified.
...
   If this bit is set in an expression, so is TREE_SIDE_EFFECTS.  */

In our case t is:
(gdb) p t
$10 = <var_decl 0x7ffff66a7cf0 meow>
(gdb) pt
warning: Expression is not an assignment (and might have no effect)
 <var_decl 0x7ffff66a7cf0 meow
    type <array_type 0x7ffff6800c78
        type <integer_type 0x7ffff6800a80 int volatile type_6 SI
            size <integer_cst 0x7ffff66bd0a8 constant 32>
            unit size <integer_cst 0x7ffff66bd0c0 constant 4>
            align 32 symtab 0 alias set -1 canonical type 0x7ffff6800a80 precision 32 min <integer_cst 0x7ffff66bd060 -2147483648> max <integer_cst 0x7ffff66bd078 2147483647>
            pointer_to_this <pointer_type 0x7ffff6800d20>>
        TI
        size <integer_cst 0x7ffff669bea0 constant 128>
        unit size <integer_cst 0x7ffff669beb8 constant 16>
        align 32 symtab 0 alias set -1 canonical type 0x7ffff6800c78
        domain <integer_type 0x7ffff6800b28 type <integer_type 0x7ffff669f1f8 sizetype>
            type_6 DI
            size <integer_cst 0x7ffff669be58 constant 64>
            unit size <integer_cst 0x7ffff669be70 constant 8>
            align 64 symtab 0 alias set -1 canonical type 0x7ffff6800b28 precision 64 min <integer_cst 0x7ffff669be88 0> max <integer_cst 0x7ffff680adc8 3>>
        pointer_to_this <pointer_type 0x7ffff68133f0>>
    side-effects addressable volatile used tree_1 decl_5 TI file /home/miyuki/gcc/test/meow/meow.cc line 15 col 18 size <integer_cst 0x7ffff669bea0 128> unit size <integer_cst 0x7ffff669beb8 16>
    align 32 context <function_decl 0x7ffff67ffe00 main>>

I.e. the condition is probably wrong and we should also check that t is an expression, not a declaration.
Comment 2 Mikhail Maltsev 2015-08-29 22:23:07 UTC
For the record: https://gcc.gnu.org/ml/gcc-patches/2015-08/msg01735.html
Comment 3 Melissa 2016-10-12 20:11:20 UTC
Seems to have been fixed sometime between 6.0 and 6.1.