[Bug c++/83912] New: [constexpr] struct with a pointer to one of its members, returned by a function, is not a constant expression

2018-01-16 Thread giu.campana at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83912

Bug ID: 83912
   Summary: [constexpr] struct with a pointer to one of its
members, returned by a function, is not a constant
expression
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: giu.campana at gmail dot com
  Target Milestone: ---

Created attachment 43159
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43159&action=edit
preprocessed source

The following source is rejected by gcc because c_3 is initialized with a
non-constant expression

=== source test.cpp 
struct MyClass
{
constexpr MyClass(int i_value)
: m_value(i_value), m_ptr(&m_value)
{ }

constexpr MyClass(const MyClass & i_source)
: m_value(i_source.m_value), m_ptr(&m_value)
{ }

MyClass & operator = (const MyClass & i_source) = delete;

  private:
int const m_value;
int const * m_ptr;
};

constexpr auto make_instance(int i_value)
{
return MyClass(i_value);
}

constexpr auto c_1 = MyClass(34);
const auto c_2 = make_instance(34);
constexpr auto c_3 = make_instance(34);
==

Tried with gcc 7.2 and gcc 8.0.1

compiled with:
g++ test.cpp -Wall -Wextra -fsanitize=undefined

output:
test.cpp:27:38: error: ‘MyClass{34, (&.MyClass::m_value)}’ is not a
constant expression
 constexpr auto c_3 = make_instance(34);

g++ -v
Using built-in specs.
COLLECT_GCC=/home/abc/gcc_dev_bin/bin/g++
COLLECT_LTO_WRAPPER=/home/abc/gcc_dev_bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc_dev/configure --prefix=/home/abc/gcc_dev_bin
--disable-multilib
Thread model: posix
gcc version 8.0.1 20180116 (experimental) (GCC)

[Bug c++/83912] [constexpr] struct with a pointer to one of its members, returned by a function, is not a constant expression

2018-01-18 Thread giu.campana at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83912

--- Comment #3 from Giuseppe Campana  ---
The snippet showed by DR2022
(http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#2022) works fine with
gcc, but it fails to compile if the object passes through a function. As
pointed out by Jakub, in these cases clang succeeds only is the object is not a
local constexpr.