views:

1254

answers:

4

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?

+2  A: 

You can declare the explicit specialisation in the same way you'd define a member function outside of its class:

class A
{
public:
  template <typename T>
  static void foo () {}
};

template <>
void A::foo<void> ()
{
}
Richard Corden
Thanks for the answer. For me it doesnt matter if they're inside a class or not; but I cant make it work either way. Is my syntax wrong or something in the code I supplied?
Rolle
The point is that you explicitly specialize the function in the namespace with a qualified function declarator. C++ doesn't allow you to re-add the 'static' keyword so you just remove it. My example above shows you how to explicitly specialize a static member.
Richard Corden
+2  A: 

Your problem appears to be with boost::function - the following specialisations work:

template <typename T>
T safeGuiCall()
{
    return T();
}

template <>
void safeGuiCall<void>()
{
}

int main() {
    int x = safeGuiCall<int>();  // ok
    //int z = safeGuiCall<void>(); // this should & does fail
    safeGuiCall<void>();   // ok
}
anon
sorry tried this but does not work. Did you compile under gcc? I think it works under VS for example...
Rolle
This is g++ version 3.4.5
anon
It's thise statics - remove them and all should be weel, it compiles with comeau, I'll update the answer
anon
+4  A: 

It's not directly an answer to your question but you can write this

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

It should work even if _f() return 'void'

Edit : In a more general case, I think we should prefer function overloading instead of specialization. Here is a good explanation for this : http://www.gotw.ca/publications/mill17.htm

Rexxar
yes very good point
I am pretty sure that wont work. T is not defined anywhere?
Rolle
@Rolle Sorry "template <typename T>" didn't survived to copy/paste.
Rexxar
+3  A: 

When you spetialize a templated method, you must do so outside of the class brackets:

template <typename X> struct Test {}; // to simulate type dependency
struct X // class declaration: only generic
{
   template <typename T>
   static void f( Test<T> );
};

// template definition:
template <typename T>
void X::f( Test<T> ) {
   std::cout << "generic" << std::endl;
}
template <>
inline void X::f<void>( Test<void> ) {
   std::cout << "specific" << std::endl;
}

int main()
{
   Test<int> ti;
   Test<void> tv;
   X::f( ti ); // prints 'generic'
   X::f( tv ); // prints 'specific'
}

When you take it outside of the class, you must remove the 'static' keyword. Static keyword outside of the class has a specific meaning different from what you probably want.

template <typename X> struct Test {}; // to simulate type dependency

template <typename T>
void f( Test<T> ) {
   std::cout << "generic" << std::endl;
}
template <>
void f<void>( Test<void> ) {
   std::cout << "specific" << std::endl;
}

int main()
{
   Test<int> ti;
   Test<void> tv;
   f( ti ); // prints 'generic'
   f( tv ); // prints 'specific'
}
David Rodríguez - dribeas
hm I tried the same thing now, but put it in a .hpp file and tried including it... then I get the error "multiple definition of vodi X:ff<void> (Test<void>). I can't see what would be the difference?
Rolle
Was it a compiler or linker error? If it was a compiler error it means that you are probably including the template header more than once and the header guards are missing, and thus the double definition: the compiler is seeing two (alas exactly exact) definitions for the implementation.
David Rodríguez - dribeas