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

  • Home
  • SEARCH
  • 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 7809111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:19:31+00:00 2026-06-02T03:19:31+00:00

I am trying to implement a generic version of the code below: #include <iostream>

  • 0

I am trying to implement a generic version of the code below:

#include <iostream>

class ContainerA
{
    public:
        ContainerA( int newData )
            : mData_(newData)
        {}
        int mData_;
};

class ContainerB
{
    public:
        ContainerB( int newData )
            : mData_(newData)
        {}
        int mData_;
};

ContainerA staticInstanceA( 3 );
ContainerB staticInstanceB( 11 );

template< ContainerA* ptrToContainer >
class WorkerOnA
{
    public:
        WorkerOnA( )
            : mPtrToContainer_(ptrToContainer)
        {}

        void operator()()
        {
            std::cout << "Data = " << mPtrToContainer_->mData_ << '\n';
        }

    private:
        ContainerA* mPtrToContainer_;
};

template< ContainerB* ptrToContainer >
class WorkerOnB
{
    public:
        WorkerOnB( )
            : mPtrToContainer_(ptrToContainer)
        {}

        void operator()()
        {
            std::cout << "Data = " << mPtrToContainer_->mData_ << '\n';
        }

    private:
        ContainerB* mPtrToContainer_;
};

int main( )
{
    WorkerOnA<&staticInstanceA> workerOnAInstance;
    WorkerOnB<&staticInstanceB> workerOnBInstance;

    workerOnAInstance();
    workerOnBInstance();

    return 0;
}

What I would like to have (if this is possible at all) is a single Worker template-class, which can be instantiated to work on either container, something like:

template< ?? ptrToContainer >
class WorkerOnAnyContainer
{
    public:
        WorkerOnA( )
            : mPtrToContainer_(ptrToContainer)
        {}

        void operator()()
        {
            std::cout << "Data = " << mPtrToContainer_->mData_ << '\n';
        }

    private:
        ?? mPtrToContainer_;
};

However, after several hours, I still can’t figure what the ‘??’s should be. Maybe a template-wizard has an idea?

Update 1: Fixed mistake in ‘operator()’ of Workers (ptrToContainer -> mPtrToContainer_). Sorry for that.

Update 2: I got something working, but I would still be curious if anyone has a better idea. For example, having a single template-parameter would be nice. Does anyone know if “template template parameters” can help in this situation?

template< class TContainer, TContainer* ptrToContainer >
class Worker
{
    public:
        Worker( )
            : mPtrToContainer_(ptrToContainer)
        {}

        void operator()()
        {
            std::cout << "Data = " << mPtrToContainer_->mData_ << '\n';
        }

    private:
        TContainer* mPtrToContainer_;
};

Thanks,
D

  • 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-06-02T03:19:33+00:00Added an answer on June 2, 2026 at 3:19 am

    I’ll give it a shot. How about changing your template so that it’s given the type as a parameter, instead of the pointer itself? You can still pass in a pointer to the constructor:

    template< typename TContainer >
    class WorkerOnAnyContainer
    {
        public:
            WorkerOnA( TContainer* ptrToContainer )
                : mPtrToContainer_(ptrToContainer)
            {}
    
            void operator()()
            {
                std::cout << "Data = " << mPtrToContainer_->mData_ << '\n';
            }
    
        private:
            TContainer* mPtrToContainer_;
    };
    

    Then you could use it like:

    WorkerOnAnyContainer<ContainerA> workerOnAInstance(&staticInstanceA);
    

    Since you want to keep the pointer-as-template-parameter design, you could go with something like this:

    template< typename TContainer, TContainer* ptrToContainer >
    class WorkerOnAnyContainer
    {
        public:
            WorkerOnA()
                : mPtrToContainer_(ptrToContainer)
            {}
    
            void operator()()
            {
                std::cout << "Data = " << ptrToContainer->mData_ << '\n';
            }
    
        private:
            TContainer* mPtrToContainer_;
    };
    

    And use it like:

    WorkerOnAnyContainer<ContainerA, &staticInstanceA> workerOnAInstance;
    

    But, this is kinda messy since you need two template arguments, and the first one feels redundant. I’m not sure it’s possible to solve this with C++03, but I figured it would be possible to build a helper method that can do the type deduction for us in C++11:

    template<typename T>
    auto CreateWorker(T* container) -> WorkerOnAnyContainer<T, container>
    {
        return WorkerOnAnyContainer<T, container>();
    }
    

    But, since the compiler expects the function to work for non-compile-time-const parameters, this doesn’t compile (GCC 4.6.3):

    use of parameter 'container' outside function body
    

    It turns out you’re not the only one trying to do this. Apparently, you can’t create a helper method this way, even with C++11.

    The only thing I can think of that actually works is to use a macro (I know, I know):

    #define CreateWorker(container) WorkerOnAnyContainer<decltype(container), &container>()
    

    Then using it is as simple as:

    auto worker = CreateWorker(staticInstanceA);    // Note no `&'
    

    This makes use of auto and a simple decltype, both C++11 features that the Intel C++ compiler supports as of v12 (though I haven’t tested this code with anything except GCC). Being a macro, it is, of course, a bit fragile though.

    See it in action!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a class that stores a generic nullable type: public class
I have a generic class that I'm trying to implement implicit type casting for.
I am trying to implement an inner class that has a generic parameterized type.
I am trying to implement a generic Wrapper-Class for Qt's class system using C#'s
After coming up against this problem myself in trying to implement a generic Vector2<int/float/double>
I am trying to implement a generic abstract class in my service layer. I
I am trying to have a List of classes that implement a generic class
I'm trying to implement generic method to put in a class a calculated value
Am trying to implement a generic way for reading sections from a config file.
I'm trying to implement a generic way of adding remote validation to xVal /

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.