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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:07:35+00:00 2026-06-10T23:07:35+00:00

class Foo1: public IFoo { public: template <class T> std::vector<T> foo() { return std::vector<T>();

  • 0
class Foo1: public IFoo
{
public:
    template <class T>
    std::vector<T> foo()
    {
        return std::vector<T>();
    }
};

class Foo2: public IFoo
{
public:
    template <class T>
    std::vector<T> foo()
    {
        return std::vector<T>();
    }
};

How can I define a common interface class for the two implementations above, such that std::vector<T> foo() is defined for this interface? Ignore that the implementations of the functions are identical.

UPDATE:

I’m writing a Container class which represents data which is sent to me via a C api.

An instance of my Container will store data of a given type, such as Container<int>, Container<std::string> and Container<Foo>.

The C api returns the data in a very awkward manner and it is possible that this will change in the future. It is possible that I can copy the data into for example std::list or std::vector, but since so much data is passed from the C api it is not known yet if this will be OK or not.

For this reason, the Container class should be independent of how the data is actually stored. I achieve this using Getter and Setter classes which I pass into the contructor, as follows:

Container<int>(Getter1<int>(uglyCApiContainer),Setter1<int>(uglyCApiContainer));

Therefore if I abandon Getter1 and Getter2 which deals with how the C api stores data, I will only need to change the creation of Containers.

However, I have a problem with this design. The type Foo.

Foo is a complex type which contains itself a set of Containers. At the moment it looks something like this:

class Foo
{
public:
        ...
    template <class V>
    Container<V> getMember(std::string& memberName)
};

So a given Foo can have a set of containers of different types. The types of these members are know to me in advance since they are stored in a model. Foo is currently a wrapper around the ugly C api memory implementation, but I would like to separate also for Foo the memory representation as I’ve done for the Container.

I’m not sure how to make Foo free of its memory implementation. One idea I had was to make getMember virtual so as to introduce perhaps different implementations but this isnt possible for templated functions.

  • 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-10T23:07:36+00:00Added an answer on June 10, 2026 at 11:07 pm

    Here’s a solution using tag dispatching and virtual inheritance:

    #include <vector>
    
    template<typename T> struct tag {};
    
    template<typename T> class IFooImpl {
    public:
        virtual std::vector<T> getImpl(tag<T>) = 0;
    };
    
    class IFoo: public virtual IFooImpl<char>, virtual IFooImpl<int>
    {
    public:
        template<typename T> std::vector<T> get() {
            return static_cast<IFooImpl<T> *>(this)->getImpl(tag<T>{});
        }
    };
    
    template<typename T>
    class FooImpl: public virtual IFooImpl<T> {
    public:
        std::vector<T> getImpl(tag<T>) { return {}; }
    };
    
    class Foo: public IFoo, FooImpl<char>, FooImpl<int> {
    };
    
    int main() {
        Foo().get<char>();
    }
    

    There’s a little bit of repetition where the supported types are covered (here char and int), but that can be avoided with variadic template inheritance:

    #include <vector>
    
    template<typename T> struct tag {};
    
    template<template<typename> class C, typename... Types> class Inherit {};
    template<template<typename> class C, typename T, typename... Rest>
    class Inherit<C, T, Rest...>: public C<T>, Inherit<C, Rest...> {};
    
    template<typename T> class IFooImplV {
    public:
        virtual std::vector<T> getImpl(tag<T>) = 0;
    };
    template<typename T> class IFooImpl: public virtual IFooImplV<T> {};
    
    template<typename... Types> class IFoo: public Inherit<IFooImpl, Types...> {
    public:
        template<typename T> std::vector<T> get() {
            return static_cast<IFooImpl<T> *>(this)->getImpl(tag<T>{});
        }
    };
    
    template<typename T> class FooImpl: public IFooImpl<T> {
    public:
        std::vector<T> getImpl(tag<T>) { return {}; }
    };
    
    template<typename... Types> class FooMulti:
        public IFoo<Types...>, Inherit<FooImpl, Types...> {};
    class Foo: public FooMulti<char, int> {};
    
    int main() {
        Foo().get<char>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two implementations of interface IFoo : Foo1 and Foo2 . I need
FooService.cs : public interface IFooService { int Foo(); } [Export(Foo1, typeof(IFooService))] public class Foo1
class A{ virtual int foo1(int a){ return foo1_1(a,filler(a)); } template<typename FunctionPtr_filler> int foo1_1(int a,
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
In Json.net we can rename the property with [JsonPropertyAttribute()] , public class Foo {
public class Foo<T> where T: Entity {} public class Foo1 : Foo<Telephone> { }
class A1{ public: A1(){} A1(const A1& rhs){} void foo() {std::cout<<Hello;} }; class A2: public
I have the following code: template <class Ret> class Foo { public: template <class
class Foo { public: Foo() { Foo(1)} Foo(int x, int y = 0):i(x) {}
class a{ public void foo(int a){ System.out.println(super); } } class b extends a{ public

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.