有什么方法可以实现这种行为吗?
template < typename... Args >
class MyClass
{
public:
typedef std::tuple < Args... > my_tuple;
template < int n >
static int bar () { return -5; };
};
我需要的是这个 - 我已经进行了可变模板 MyClass
包含方法 foo
它是由另一种类型模板化的(在我的例子中只有整数)。这可能吗?我找到了类似的解决方案,但仅适用于非可变参数类。
但是由于bar
而无法编译.
编辑: 我在 gcc 4.7.2 上编译
应该运行这样的方法 MyClass<int, int>::bar<4>()
有人可以帮助我吗?
提前致谢
EDIT2:完整代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <map>
template < typename... Args >
class MyClass
{
public:
typedef std::tuple < Args... > my_tuple;
template < int n >
static int bar () { return -5; };
};
template < class A, int N >
static void foo()
{
A::bar < N > ();
}
int main() {
foo< MyClass<int, int>, 4> ();
return 0;
}
编辑3:错误
$g++ -Wall -std=c++11 -g -o test.out test.cpp
test.cpp: In function ‘void foo()’:
test.cpp:28:16: error: expected primary-expression before ‘)’ token
test.cpp: In instantiation of ‘void foo() [with A = MyClass<int, int>; int N = 4]’:
test.cpp:34:30: required from here
test.cpp:28:2: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
make: *** [test] Error 1
最佳答案
关于c++ - 可变模板类中的模板方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873493/