Take a look at the code:
#include <iostream> #include <utility> class test { private: test() { } public: test foo() { return *this; } static const char *name() { return "test"; } }; int main() { std::cout << decltype(test().foo())::name() << std::endl; // 1 std::cout << decltype(std::declval<test>().foo())::name() << std::endl; // 2 }
I was expecting the line // 1 could not be compiled because the default constructor test is private.
However, it works well. I tested it on my g ++ 4.8.3 with -Wall -Wextra -Werror -pedantic at a loss, but it works fine without any errors or warnings.
(Also, it seems to work well in GCC 4.9.1.)
From this page , I think we can use the default private constructor if the expression is not evaluated. So, I checked the following to verify this.
#include <iostream> #include <utility> class test { private: test(int) { } public: test foo() { return *this; } static const char *name() { return "test"; } }; int main() { std::cout << decltype(test().foo())::name() << std::endl; // 1 std::cout << decltype(std::declval<test>().foo())::name() << std::endl; // 2 }
(live example)
As expected, it was not compiled.
But .... why ?? How is this possible? Can we use private elements in an invaluable expression? Or is there a special rule for default constructors? Could you explain to me why?
c ++ language-lawyer private-members c ++ 11 decltype
ikh
source share