1

I just encountered the following code, where the first static_assert will not fail if the templaterized pred1 is used, and in main x is declared with an empty list of initializers. Is this something conforming to the standard, or is it a compiler bug? I am using g++ 6.4.1 on Fedora 25.

#include <cstdio>

template<class T> struct pred1 {
    constexpr static bool value = false;
};

struct pred2 {
    constexpr static bool value = false;
};

template<class A> struct align {
    // this assertion only fails with align<int> x; but not with align<int> x();
    static_assert( pred1<A>::value == true, "fail1" );

    // this assertion will fail anyway
    // static_assert( pred2::value == true, "fail2" );
};

int main() {

    align<int> x; // this fails
    align<int> x {}; // this fails too
    align<int> x(); // this does not fail

    return 0;
}
Rainn
  • 315
  • 1
  • 9
  • 6
    Read about the "most vexing parse". – nwp Nov 03 '17 at 21:26
  • 3
    `align x()` doesn't do what you think it does; it declares a function `x` taking no arguments which returns `align` – Justin Nov 03 '17 at 21:26
  • 1
    [Clang warns about this](https://wandbox.org/permlink/yWFqHahsNrjUaTY7). It's a good idea to test with multiple compilers when possible. – chris Nov 03 '17 at 21:28
  • @nwp Interesting! How was it possible that I never ran into this before aftering years of coding C++.... – Rainn Nov 03 '17 at 21:31
  • @Justin Got it. Thanks! – Rainn Nov 03 '17 at 21:31
  • @chris Just tried and the warning is easy to understand. Thanks for the suggestion! – Rainn Nov 03 '17 at 21:33
  • @Rainn - *How was it possible that I never ran into this before aftering years of coding C++* -- `struct foo{}; int main() { foo f(); }` -- That does not construct a `foo` object named `f`. – PaulMcKenzie Nov 03 '17 at 21:37

0 Answers0