fork download
  1. #include <tuple>
  2. #include <cstddef>
  3. #include <utility>
  4. #include <iostream>
  5.  
  6. enum class XParameters : unsigned int
  7. {
  8. PARAMETER1, // int
  9. PARAMETER2, // float
  10. MAX,
  11. };
  12.  
  13. enum class YParameters : unsigned int
  14. {
  15. PARAMETER3 = XParameters::MAX // std::string
  16. };
  17.  
  18. using XTuple = std::tuple<int, float>;
  19. using YAttributes = std::tuple<std::string>;
  20. using YTuple = decltype(tuple_cat(XTuple(), YAttributes()));
  21.  
  22. template <typename Attributes>
  23. struct Common
  24. {
  25. Common(Attributes&& attr) : attributes(std::move(attr)) {}
  26.  
  27. Attributes attributes;
  28.  
  29. template <typename AttributeName>
  30. auto GetAttribute(AttributeName attributeName) -> decltype(std::declval(std::get<static_cast<size_t>(attributeName)>(attributes)))
  31. {
  32. return std::get<static_cast<size_t>(attributeName)>(attributes);
  33. }
  34. };
  35.  
  36. struct X : Common<XTuple>
  37. {
  38. X() : Common(std::make_tuple(42, 3.14f)) {}
  39. };
  40.  
  41. struct Y : Common<YTuple>
  42. {
  43. Y() : Common(std::make_tuple(666, 0.01f, "string")) {}
  44. };
  45.  
  46. int main()
  47. {
  48. X x;
  49. Y y;
  50.  
  51. int parameter1 = std::get<static_cast<size_t>(XParameters::PARAMETER1)>(x.attributes); // Compiles, works.
  52. std::cout << parameter1 << std::endl;
  53.  
  54. std::string parameter3 = std::get<static_cast<size_t>(YParameters::PARAMETER3)>(y.attributes); // Compiles, works.
  55. std::cout << parameter3 << std::endl;
  56.  
  57. x.GetAttribute(XParameters::PARAMETER1); // Does not compile.
  58.  
  59. //parameter3 = std::get<static_cast<size_t>(YParameters::PARAMETER3)>(x.attributes); // Does not compile, phew...
  60.  
  61.  
  62.  
  63.  
  64. return 0;
  65. }
Compilation error #stdin compilation error #stdout 0s 3456KB
stdin
Standard input is empty
compilation info
prog.cpp:30:119: error: invalid operands of types '<unresolved overloaded function type>' and 'size_t {aka unsigned int}' to binary 'operator<'
     auto GetAttribute(AttributeName attributeName) -> decltype(std::declval(std::get<static_cast<size_t>(attributeName)>(attributes)))
                                                                                                                       ^
prog.cpp:30:119: error: invalid operands of types '<unresolved overloaded function type>' and 'size_t {aka unsigned int}' to binary 'operator<'
prog.cpp: In function 'int main()':
prog.cpp:57:4: error: 'struct X' has no member named 'GetAttribute'
  x.GetAttribute(XParameters::PARAMETER1); // Does not compile.
    ^
stdout
Standard output is empty