gpt4 book ai didi

c++ - 可变模板化类中的模板化方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:09 24 4
gpt4 key购买 nike

<分区>

有什么办法可以实现这种行为吗?

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com