c++ - 模板类中的静态非模板方法

标签 c++ templates c++11

我想向模板类添加一个静态函数,无需先传递模板参数即可访问该函数。这可能吗?

namespace foo {
  template <typename T>
  class bar {
  public:
    static void eggs();
  };
}

foo::bar<some_t>::eggs();  // works
foo::bar::eggs();  // does not work

我想避免将 eggs() 移动到 foo 命名空间或为它创建一个新的命名空间(例如 foo::bar_::eggs(),呃)。

最佳答案

没有。这不是模板类的工作方式。你想做的事在 C++ 中是不可能的。

关于c++ - 模板类中的静态非模板方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168119/

相关文章:

c++ - 无法在 C++ 中通过自动迭代映射

c++ - 二进制 * 的无效操作数

c++ - "template"消歧器是 c++11。 VS2008 编译器有机会吗?

C++ : display a bar chart of array data using *

c++ - 当正则表达式匹配重叠时会发生什么?

c++11 - 在 std::valarray<T> 上使用 .sum() 和 +=

python - 传递给 C 的 SWIG 生成对象的内存管理

c++ - 警告 665 : Unparenthesized parameter 2 in macro 'SET_RECORD' is passed an expression

c++ - 策略类设计但没有将整个用户类作为模板

javascript - meteor : best practice for modifying Mongo query result before template takes it?