template specialization for static member functions; howto?

Posted by Rolle on Stack Overflow See other posts from Stack Overflow or by Rolle
Published on 2009-04-20T09:00:10Z Indexed on 2010/04/27 11:13 UTC
Read the original article Hit count: 188

Filed under:
|
|

I am trying to implement a template function with handles void differently using template specialization.

The following code gives me an "Explicit specialization in non-namespace scope" in gcc:

template <typename T>
static T safeGuiCall(boost::function<T ()> _f)
{
	if (_f.empty())
		throw GuiException("Function pointer empty");
	{
		ThreadGuard g;
		T ret = _f();
		return ret;
	}
}

// template specialization for functions wit no return value
template <>
static void safeGuiCall<void>(boost::function<void ()> _f)
{
	if (_f.empty())
		throw GuiException("Function pointer empty");
	{
		ThreadGuard g;
		_f();
	}
}

I have tried moving it out of the class (the class is not templated) and into the namespace but then I get the error "Explicit specialization cannot have a storage class". I have read many discussions about this, but people don't seem to agree how to specialize function templates. Any ideas?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates