c++ - Using inner class with CRTP -
is there possibility use inner class or enum crtp? ex.
template<typename container> struct containerbase { std::map<typename container::enum, int> _; }; struct concretecontainer : containerbase<concretecontainer> { enum class enum { left, right }; };
no. within class template derived class isn't defined yet (that's not complete object in standardese).
in fact (working draft):
a class considered completely-defined object type (or complete type) @ closing
}
therefore cannot expect able access 1 of members or whatever declared in derived class within class template.
you can work around passing enum separately, requires define enum somewhere else (another base class? outer scope? whatever...). can work around using traits classes. , on.
there several alternatives that, cannot access directly enum defined in derived class.
here example of viable solution:
#include <map> template<typename> struct traits; template<typename container> struct containerbase { std::map<typename traits<container>::enum, int> _; }; template<> struct traits<struct concretecontainer> { enum class enum { left, right }; }; struct concretecontainer : containerbase<concretecontainer> {}; int main() { concretecontainer cc; cc._[traits<concretecontainer>::enum::left] = 0; cc._[traits<concretecontainer>::enum::right] = 1; }
see , running on wandbox.
wiki
Comments
Post a Comment