Files
linux-kernel-module-cheat/userland/cpp/most_vexing_parse.cpp
Ciro Santilli 六四事件 法轮功 75b081f171 cpp: most vexing parse
2020-02-27 00:00:04 +00:00

58 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#include "common.hpp"
int main() {
struct C {
int i;
C() : i(1) {}
C(int i) : i(i) {}
};
struct D {
D() {}
};
// Declares *FUNCTION* called `c` that returns `C` inside function main.
//
// This is the same as in C, where it is possible to declare a function from inside another function,
// but not define it.
//
// Therefore there would be not way for C++ to distinguish between the two,
// and still be backwards compatible with C.
{
C c();
#if 0
// ERROR: function definition is not possible inside another function.
C c() {return C();}
#endif
//c.i;
}
// If you want to call a default constructor, use:
{
C c;
assert(c.i == 1);
}
// For non-default constructors, literal arguments disambiguate
// things as this syntax could not possibly be a function declaration.
{
C c(2);
assert(c.i == 2);
}
// But sometimes even arguments are not enough: here D()
// could matn that the declared `c`
{
C c(D());
#if 0
// error: request for member i in c, which is of non-class type main()::C(main()::D (*)())
assert(c.i == 2);
#endif
}
}