Bug 56970 - SFINAE does not apply correctly to sizeof.
Summary: SFINAE does not apply correctly to sizeof.
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: 4.8.1
Assignee: Paolo Carlini
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-04-15 14:36 UTC by Fan Yu
Modified: 2013-04-24 15:43 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.7.3
Known to fail:
Last reconfirmed: 2013-04-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fan Yu 2013-04-15 14:36:27 UTC
This code compiles with g++ 4.7.3, clang 3.2, but not g++ 4.8.
Error is:
In substitution of ‘template<class C> static constexpr int has<T>::test(decltype (sizeof (C:: x))) [with C = C; T = foo] [with C = foo]’:
required from ‘const int foobar<foo>::value’
required from here
error: invalid use of non-static member function ‘int foo::x()’

#include <string>
#include <iostream>
template <typename T>
struct has {
    template <typename>
    constexpr static int test(...) {
      return 0;
    }
    template <typename C>
    constexpr static int test(decltype(sizeof(C::x))) {  // Doesn't compile.
      return 1;   // Is a member variable.
    }
    template <typename C, int c =
        sizeof(decltype(((C*)nullptr)->x()))>
    constexpr static int test(int) {
      return 2;   // Is a member function.
    }
    static const int value = test<T>(0);
};
struct foo {
    int x();
};
struct bar {
    int x;
};
int main() {
    std::cout << has<int>::value << std::endl;
    std::cout << has<foo>::value << std::endl;
    std::cout << has<bar>::value << std::endl;
}
Comment 1 Jakub Jelinek 2013-04-15 16:50:36 UTC
Started with http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190830
By using __builtin_printf or static_assert one can avoid all the includes.
Comment 2 Paolo Carlini 2013-04-24 10:01:53 UTC
Seems simple: missing propagation of complain.
Comment 3 Paolo Carlini 2013-04-24 15:43:16 UTC
Fixed mainline and 4.8.1.