【问题标题】:Alias within a class template类模板中的别名
【发布时间】:2022-12-03 17:46:15
【问题描述】:

For following class

template<typename T>
class test { 
   public:
   using unit = std::micro;    
};

How do I access unit like test::unit without having to specify the template argument or make it a template alias. Please note that inserting a dummy template argument like e.g . int is not an option since some template classes cannot be instantiated with such types.

  • you move it out of the template
  • xy problem. Why is it a member of the template when you do not want/need to instantiate the template to access it?
  • A general approach you can take is to create a common base class from which the template classes inherit, template&lt;typename T&gt; class Test : public TestBase, and then put everything they have in common in the base class.
  • in a nutshell, template&lt;typename T&gt; means \"everything that follows depends on T\"
  • If you want do not want it in the enclosing namespace, move it to a non-templated baseclass, e.g. test_defs and then test can inherit from it.

标签: c++


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多