Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of class template requires template argument list

Tags:

c++

templates

I moved out the methods implementation from my class and caught the following error:

use of class template requires template argument list 

for method whitch doesn't require template type at all... (for other methods all ok)

Class

template<class T> class MutableQueue { public:     bool empty() const;     const T& front() const;     void push(const T& element);     T pop();  private:     queue<T> queue;     mutable boost::mutex mutex;     boost::condition condition; }; 

Wrong implementation

template<>   //template<class T> also incorrect bool MutableQueue::empty() const {     scoped_lock lock(mutex);     return queue.empty(); } 
like image 835
Torrius Avatar asked Nov 20 '12 15:11

Torrius


People also ask

What are template arguments?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

CAN default arguments be used with the template?

You cannot give default arguments to the same template parameters in different declarations in the same scope. The compiler will not allow the following example: template<class T = char> class X; template<class T = char> class X { };

Can a template be a template parameter?

Templates can be template parameters. In this case, they are called template parameters. The container adaptors std::stack, std::queue, and std::priority_queue use per default a std::deque to hold their arguments, but you can use a different container.

How many template arguments are there?

1) A template template parameter with an optional name. 2) A template template parameter with an optional name and a default. 3) A template template parameter pack with an optional name.


1 Answers

It should be:

template<class T> bool MutableQueue<T>::empty() const {     scoped_lock lock(mutex);     return queue.empty(); } 

And if your code is that short, just inline it, as you can't separate the implementation and header of a template class anyway.

like image 189
xiaoyi Avatar answered Sep 23 '22 12:09

xiaoyi