Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:40:10+00:00 2026-05-11T16:40:10+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:40:11+00:00Added an answer on May 11, 2026 at 4:40 pm

    When you specialize 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'
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't want the email helper but rather the email… May 12, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer There are too many GUI toolkits for Java since the… May 12, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer Set this function up into your bootstrap file or wherever… May 12, 2026 at 10:11 pm

Related Questions

I am trying to implement a template class that would be able to tell
I am trying to implement a List class using pointers and am trying to
I am working on a group senior project for my university and I have
I am using C++ and I am trying to create a templated class (a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.