Bug 61806 - [C++11] Expression sfinae w/o access gives hard error in partial template specializations
Summary: [C++11] Expression sfinae w/o access gives hard error in partial template spe...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 8.2
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
: 80475 (view as bug list)
Depends on:
Blocks: 59002
  Show dependency treegraph
 
Reported: 2014-07-15 09:10 UTC by Daniel Krügler
Modified: 2021-04-28 21:08 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-05-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Krügler 2014-07-15 09:10:36 UTC
The following code, compiled with gcc 4.10.0 20140714 (experimental) using the flags

-Wall -Wextra -std=c++11 -pedantic

or - alternatively -

-Wall -Wextra -std=c++1y -pedantic

//-----------------------
struct true_type 
{
  static const bool value = true;
};

struct false_type 
{
  static const bool value = false;
};

template<class T>
T&& declval();

template<typename> struct check { typedef void type; };

template<typename T, typename Enable = void>
struct has_public_f : false_type {};

template<typename T>
struct has_public_f<
    T,
    typename check<
        decltype(
            declval<T&>().f()
        )
    >::type
> : true_type {};

struct Spub  { public: void f(); };
struct Spriv { private: void f(); };

static_assert( has_public_f<Spub>::value, "Ouch");
static_assert(!has_public_f<Spriv>::value, "Ouch");

int main() {}
//-----------------------

is rejected with the following diagnostics:

<quote>
prog.cc: In instantiation of 'struct has_public_f<Spriv>': 
prog.cc:33:35: required from here 
prog.cc:30:30: error: 'void Spriv::f()' is private 
  struct Spriv { private: void f(); }; 
                               ^ 
prog.cc:27:15: error: within this context 
  > : true_type {}; 
                ^ 
prog.cc:30:30: error: 'void Spriv::f()' is private 
  struct Spriv { private: void f(); }; 
                               ^ 
prog.cc:27:15: error: within this context 
  > : true_type {}; 
                ^ 
prog.cc:30:30: error: 'void Spriv::f()' is private 
  struct Spriv { private: void f(); }; 
                               ^ 
prog.cc:33:16: error: within this context     
  static_assert(!has_public_f<Spriv>::value, "Ouch"); 
                 ^ 
prog.cc:33:1: error: static assertion failed: Ouch 
  static_assert(!has_public_f<Spriv>::value, "Ouch"); 
  ^
</quote>

It seems that in this context there is no silent rejection of the partial specialization, albeit it should.
Comment 1 Ville Voutilainen 2014-12-14 00:49:42 UTC
Clang accepts the code.
Comment 2 Jonathan Wakely 2017-04-20 17:48:24 UTC
EDG accepts it too.
Comment 3 gcc-bugs 2017-09-25 13:52:16 UTC
I think I encountered a variant of this bug.

Using this new awesome -fconcept feature, you can do the following:

```
template <typename type_t>
struct type_trait;

template <>
struct type_trait<int>
{
    static constexpr auto length = 0;
};

template <>
struct type_trait<long>
{
private:
    static constexpr auto length = 0;
};

template <typename type_t>
concept bool has_length = requires(type_t a)
{
    { type_trait<type_t>::length };
};

int main()
{
    static_assert(!has_length<char>); // expect: false, has no ::length
    static_assert(has_length<int>); // expect: true, has ::length
    static_assert(!has_length<long>); // expect: false, ::length is non-visible
    // but, last one fails in a compiler error
    return 0;
}
```

This example asks whether a type_trait is defined for a given type. And it would be super useful to be able to express this.

I think gcc uses internally SFINAE to check this but unfortunately fails because of this bug (probably).
Comment 4 Jonathan Wakely 2018-05-22 14:56:23 UTC
This is still present on trunk, and causing issues for libc++ when trying to use void_t expressions in deduction guides.
Comment 6 Jason Merrill 2018-06-04 15:16:32 UTC
Author: jason
Date: Mon Jun  4 15:16:00 2018
New Revision: 261151

URL: https://gcc.gnu.org/viewcvs?rev=261151&root=gcc&view=rev
Log:
	PR c++/61806 - missed SFINAE with partial specialization.

	* cp-tree.h (deferring_access_check_sentinel): Add deferring_kind
	parameter to constructor.
	* pt.c (instantiate_class_template_1): Enable access checking
	before call to most_specialized_partial_spec.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/sfinae63.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/pt.c
Comment 7 Jason Merrill 2018-06-11 22:02:55 UTC
Author: jason
Date: Mon Jun 11 22:02:19 2018
New Revision: 261464

URL: https://gcc.gnu.org/viewcvs?rev=261464&root=gcc&view=rev
Log:
	PR c++/61806 - missed SFINAE with partial specialization.

	* cp-tree.h (deferring_access_check_sentinel): Add deferring_kind
	parameter to constructor.
	* pt.c (instantiate_class_template_1): Enable access checking
	before call to most_specialized_partial_spec.

Added:
    branches/gcc-8-branch/gcc/testsuite/g++.dg/cpp0x/sfinae63.C
Modified:
    branches/gcc-8-branch/gcc/cp/ChangeLog
    branches/gcc-8-branch/gcc/cp/cp-tree.h
    branches/gcc-8-branch/gcc/cp/pt.c
Comment 8 Jason Merrill 2018-06-12 18:46:38 UTC
Fixed for 8.2.
Comment 9 Patrick Palka 2021-04-28 21:08:49 UTC
*** Bug 80475 has been marked as a duplicate of this bug. ***