2011-11-03 79 views
2

我有以下的小測試應用程序,在那裏我試圖採用使用聲明中模板。模板「使用」聲明沒有工作

但是,它不能編譯。 (我用gcc 4.6.1)

src/main.cpp:36:3: error: expected unqualified-id before ‘using’

  • 是我使用聲明中無效的C++?
  • 是否有可能不指定爲模板參數之一的類型來創建一個模板別名嗎?
  • 是否有可能創建一個模板別名帶可變參數模板?

任何瞭解不勝感激!

#include <iostream> 
#include <utility> 

template<typename F, typename... Args> 
struct invoke; 

// specialisation of invoke for 1 parameter 
template<typename F, typename A0> 
struct invoke<F, A0> 
{ 
    invoke(F& f_, A0&& a0_) 
    : _f(f_) 
    , _a0(std::move(a0_)) 
    {} 

    void operator()() 
    { 
    _f(_a0); 
    } 

    F _f; 
    A0 _a0; 
}; 

template<typename F> 
struct traits; 

// fwd declaration for handler 
struct handler; 

// specialisation of traits for handler 
template<> 
struct traits<handler> 
{ 
    template<class F, typename... Args> 
    using call_t = invoke<F, Args...>;    // line 36 
}; 

template<typename F> 
struct do_it 
{ 
    template<typename... Args> 
    void operator()(F& _f, Args... args) 
    { 
    // create an object of the type declared in traits, and call it 
    typename traits<F>::template call_t<F, Args...> func(_f, std::forward<Args>(args)...); 
    func(); 
    } 
}; 

struct handler 
{ 
    void operator()(int i) 
    { 
    std::cout << i << std::endl; 
    } 
}; 

int main() 
{ 
    handler h; 

    do_it<handler> d; 
    d(h, 4); 

    return 0; 
} 

回答