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 6327431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:16:43+00:00 2026-05-24T17:16:43+00:00

The class Widget has some functions that apply for all parameter types (common functions)

  • 0

The class Widget has some functions that apply for all parameter types (common functions) and other functions that need to be specialized for given types (the uncommon functions).

g++ insists that the specialization for Widget should also define common_fn() and not just uncommon_fn(), but that defeats the purpose of using specialization in the first place. How can one avoid repeating common_fn()?

#include <cassert>

template<typename Type> struct Widget
{
    Widget() {}
    char common_fn() { return 'a'; }
    int uncommon_fn() { return 1; }
};

template<> struct Widget<char>
{
    Widget() {}
    int uncommon_fn() { return 2; }
};

int main()
{
    Widget<char> WidgetChar;
    assert( WidgetChar.common_fn() == 'a' ); // Error
    assert( WidgetChar.uncommon_fn() == 2 );
}

begin-edit

to Alf:

I am unable to use

template<> int Widget<char>::uncommon_fn() { return 2; }

because some of the uncommon functions need to return a trait type (and so it was excessive to simplify by making the actual type primitive).

Or is there in fact a way to make the compiler recognize typename Foo::Bar when writing

struct Foo { typedef FooBar Bar; };
template<> typename Foo::Bar Widget<Foo>::uncommon_fn() { return ....; }

?

end-edit

begin-edit2

to iammilind:

That’s interesting, but I am unable to use derivation from Widget (or the possibly clearer solution of refactoring the common parts into a parent class GeneralWidget) for the same reason. The common parts are not entirely common. Their declarations and their definitions look the same, but because they use traits they are at the end quite different.

end-edit2

  • 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-24T17:16:44+00:00Added an answer on May 24, 2026 at 5:16 pm
    #include <assert.h>
    
    template<typename Type> struct Widget
    {
        Widget() {}
        char common_fn() { return 'a'; }
        int uncommon_fn() { return 1; }
    };
    
    template<>
    int Widget<char>::uncommon_fn() { return 2; }
    
    int main()
    {
        Widget<char> WidgetChar;
        assert( WidgetChar.common_fn() == 'a' ); // OK
        assert( WidgetChar.uncommon_fn() == 2 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is that in my Widget class i've the following declaration: MouseEvent* X;
I have a stl::list containing Widget class objects. They need to be sorted according
I have some calls that must execute sequentially. Consider an IService that has a
I have a page that has a bunch of widgets on it. Each widget
I created a widget that is a subclass of Composite and has a com.extjs.gxt.ui.client.widget.Viewport
I have a class (in C++), call it Data , that has thousands of
I have an interface that has ~16 input field boxes. They are all declared
I'm writing an application using gtkmm. I wrote a simple widget class, that I
class Widget { public: Widget() { cout<<~Widget()<<endl; } ~Widget() { cout<<~Widget()<<endl; } void* operator
I've got the following custom widget: class QDESIGNER_WIDGET_EXPORT MySpecialButton : public QAbstractButton { Q_OBJECT

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.