[Bug c++/95407] New: G++ allows access to base class members from a friend of a derived class

2020-05-29 Thread dragondreamer at live dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95407

Bug ID: 95407
   Summary: G++ allows access to base class members from a friend
of a derived class
   Product: gcc
   Version: 10.1.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: dragondreamer at live dot com
  Target Milestone: ---

G++ (at least trunk, 10.1.0, 9.3, 9.2) successfully compiles the following
code:

// ===
#include 

struct test_base
{
protected:
static constexpr int value = 123;
};

template
struct ignored_friend_declaration
{
static void bug()
{
std::cout << Base::value;
}
};

struct test_child : test_base
{
using ignored_friend_declaration_t = ignored_friend_declaration;
friend ignored_friend_declaration_t;

static void test()
{
ignored_friend_declaration_t::bug();
}
};

int main()
{
test_child::test();
}
// ===

According to http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1873,
access to Base::value should be disallowed. Clang produces the following error
message: :14:28: error: 'value' is a protected member of 'test_base'.

See also https://bugs.llvm.org/show_bug.cgi?id=46036

[Bug c++/95269] New: Lambda is allowed to capture any constexpr variable without specifying any captures

2020-05-22 Thread dragondreamer at live dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95269

Bug ID: 95269
   Summary: Lambda is allowed to capture any constexpr variable
without specifying any captures
   Product: gcc
   Version: 10.1.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: dragondreamer at live dot com
  Target Milestone: ---

G++ (at least these versions: 8.2.1, 9.2.1, 10.1.0, trunk) successfully
compiles the following code:

// ===
template
constexpr void test(Lambda lambda)
{
constexpr auto result = lambda();
static_assert(result.value == 1);
}

struct teststruct
{
int value = 0;
};

constexpr auto get_value()
{
teststruct result{};
result.value = 1;
return result;
}

int main()
{
constexpr auto value = get_value();
//Here's a lambda without any captures
auto options_lambda = []() { return value; };
test(options_lambda);
}
// ===

This code instead should produce an error when trying to create a lambda which
uses "value" without capturing it. This is the exact behavior of Clang and
MSVC, which both produce an error message.

As far as I understand, the following rule must be applied here:
"If a lambda expression (or an instantiation of a generic lambda's function
call operator) ODR-uses this or any variable with automatic storage duration,
it must be captured by the lambda expression."

[Bug c++/66639] declare __func__ , __FUNCTION__ & __PRETTY_FUNCTION__ as constexpr

2017-03-24 Thread dragondreamer at live dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66639

D  changed:

   What|Removed |Added

 CC||dragondreamer at live dot com

--- Comment #8 from D  ---
This bug is not still yet for __PRETTY_FUNCTION__. The sample code below does
not compile even with GCC 7 (however, works fine in Clang).

//

constexpr bool str_equal(const char* s1, const char* s2) noexcept
{
   return !*s1 && !*s2 ? true
: !*s1 ? false
: !*s2 ? false
: *s1 == *s2 ? str_equal(s1 + 1, s2 + 1)
: false;
}

int main()
{
static_assert(str_equal(__PRETTY_FUNCTION__, "int main()"), "test");
}


//



: In function 'int main()':
:12:5: error: non-constant condition for static assertion
 static_assert(str_equal(__PRETTY_FUNCTION__, "int main()"), "test");
 ^
:12:28:   in constexpr expansion of 'str_equal(((const char*)(&
__PRETTY_FUNCTION__)), ((const char*)"int main()"))'
:12:5: error: the value of '__PRETTY_FUNCTION__' is not usable in a
constant expression
:12:29: note: '__PRETTY_FUNCTION__' was not declared 'constexpr'
 static_assert(str_equal(__PRETTY_FUNCTION__, "int main()"), "test");
 ^~~
Compiler exited with result code 1