msvc_typeof_in_lambda.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2018 Tobias Loew
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/config.hpp>
  5. #include <boost/config/pragma_message.hpp>
  6. #if defined(BOOST_NO_CXX11_LAMBDAS)
  7. BOOST_PRAGMA_MESSAGE("Skipping test due to BOOST_NO_CXX11_LAMBDAS")
  8. int main() {}
  9. #elif defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
  10. BOOST_PRAGMA_MESSAGE("Skipping test due to BOOST_NO_CXX11_AUTO_DECLARATIONS")
  11. int main() {}
  12. #else
  13. #include <boost/typeof/typeof.hpp>
  14. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  15. namespace detail {
  16. template<class T> inline T& deref(T& r) {
  17. return r;
  18. }
  19. template<class T>
  20. struct wrapper {
  21. typedef T type;
  22. };
  23. template<class T> wrapper<T> wrap(T&);
  24. };
  25. BOOST_TYPEOF_REGISTER_TEMPLATE(::detail::wrapper, 1)
  26. void test_typeof_in_lambda() {
  27. // Visual Studio 2015 (BOOST_MSVC == 1900) had an internal compiler error with Boost 1.65 and 1.66 when using BOOST_SCOPE_EXIT inside a lambda
  28. // the error was due to a change of include in boost/typeof/typeof.hpp (<boost/typeof/decltype.hpp> instead of <boost/typeof/native.hpp>)
  29. // This test is an more or less minimal extract from the BOOST_SCOPE_EXIT macro expansions
  30. // worked also with VS 2015 in version 1.65/1.66
  31. int n;
  32. typedef BOOST_TYPEOF(::detail::wrap(::detail::deref(n))) n_type_wrapped;
  33. typedef n_type_wrapped::type n_type;
  34. int test;
  35. auto check_property = [&n,&test]() {
  36. // internal compiler error with VS 2015 in version 1.65/1.66
  37. // minimal extract from
  38. //BOOST_SCOPE_EXIT(test) {
  39. // test = 42;
  40. //}BOOST_SCOPE_EXIT_END
  41. // this compiles always (as long as the one before outside the lambda has the same name)
  42. typedef BOOST_TYPEOF(::detail::wrap(::detail::deref(n))) n_type_wrapped;
  43. typedef n_type_wrapped::type n_type;
  44. // this fails with internal compiler error with VS 2015 in version 1.65/1.66
  45. typedef BOOST_TYPEOF(::detail::wrap(::detail::deref(test))) test_type_wrapped;
  46. typedef test_type_wrapped::type test_type;
  47. };
  48. }
  49. int main() {
  50. test_typeof_in_lambda();
  51. return 0;
  52. }
  53. #endif