2014-04-04 33 views
2

有什麼方法可以實現這種行爲嗎?可變模板類中的模板方法

template < typename... Args > 
class MyClass 
{ 
public: 
    typedef std::tuple <Args...> my_tuple; 


    template < int n > 
    static int bar() { return -5; }; 

}; 

我需要的是這樣的 - 我已經variadically模板MyClass包含方法foo這是由另一種類型的模板(在我的情況下,僅整數)。這甚至有可能嗎?我發現了類似的解決方案,但僅適用於非可變類。

但是由於bar的原因無法編譯。

編輯: 我編譯海合會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; 
} 

EDIT3:錯誤

$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 
+1

你的意思方法'bar'? – pippin1289

+1

代碼中沒有'foo'。你的代碼在Clang 3.4上編譯得很好。 – 0x499602D2

+0

也可以用'-std = C++ 0x'在gcc 4.7.2上編譯好。 – Chnossos

回答

2

的問題是,你調用從模板中的模板方法,無需消除歧義的語法作爲模板調用(請參閱duplicate以獲得很好的解釋)。

您需要template預選賽:

A::template bar<N>();