2014-05-17 47 views
1
我有問題,編譯下面的成員定義

數據成員「queryCallback」不能是一個成員模板

template<typename... A> 
std::tuple<std::string, std::function<void(A...)> > queryCallback; 

錯誤是在標題中說:

/databasedispatcher.h:14: error: data member 'queryCallback' cannot be a member template 

我意識到我不能爲不是方法/函數的成員使用模板定義。考慮到這個情況,我怎樣才能使用<A...>

謝謝。

+0

定義模板別名和使querycallback是別名的特定實例的變量。模板變量將成爲C++ 14的一部分,但我不知道它們的侷限性。 – Manu343726

回答

2

使一個受過教育的猜測,你需要將它定義爲模板別名:

template<typename... A> 
using queryCallback = std::tuple<std::string, std::function<void(A...)>>; 

代碼示例:

#include <tuple> 
#include <iostream> 
#include <string> 
#include <functional> 

template<typename... A> 
using queryCallback = std::tuple<std::string, std::function<void(A...)>>; 

int main() 
{ 
    auto foo = [](int a, int b) { std::cout << a << " + " << b << " = " << a + b << std::endl; }; 
    queryCallback<int, int> A("foo", foo); 

    std::cout << std::get<0>(A) << std::endl; 
    std::get<1>(A)(2, 2); 

    return 0; 
} 

輸出:

2 + 2 = 4

0

使用alias template

template<typename... B> 
struct T 
{ 
    template<typename... A> using QueryCallbackType = std::tuple<std::string, std::function<void(A...)>>; 
    QueryCallbackType<B...> query_callback; 
};