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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:16:37+00:00 2026-05-13T08:16:37+00:00

I’m writing a template for which I’m trying to provide a specialization on a

  • 0

I’m writing a template for which I’m trying to provide a specialization on a class which itself is a template class. When using it I’m actually instanciating it with derivitives of the templated class, so I have something like this:

template<typename T> struct Arg
{
    static inline const size_t Size(const T* arg) { return sizeof(T); }
    static inline const T*     Ptr (const T* arg) { return arg; }
};

template<typename T> struct Arg<Wrap<T> >
{
   static inline const size_t Size(const Wrap<T>* arg) { return sizeof(T); }
   static inline const T*     Ptr (const Wrap<T>* arg) { return arg.Raw(); }
};

class IntArg: public Wrap<int>
{
    //some code
}

class FloatArg: public Wrap<float>
{
    //some code
}
template<typename T>
void UseArg(T argument)
{
    SetValues(Arg<T>::Size(argument), Arg<T>::Ptr(&argument));
}

UseArg(5);
UseArg(IntArg());
UseArg(FloatArg());

In all cases the first version is called. So basically my question is: Where did I went wrong and how do I make him call the the version which returns arg when calling UseArg(5), but the other one when calling UseArg(intArg)? Other ways to do something like this (without changing the interface of UseArg) are of course welcome to.

As a note the example is a little simplyified meaning that in the actual code I’m wrapping some more complex things and the derived class has some actual operations.

  • 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-13T08:16:37+00:00Added an answer on May 13, 2026 at 8:16 am

    I think there are three approaches:

    1) Specialize Arg for derived types:

    template <typename T> struct Arg ...
    template <> struct Arg <IntArg> ...
    template <> struct Arg <FloatArg> ...
    // and so on ...
    

    This sucks, because you can’t know in advance what types you will have. Of course you you can specialize once you have these types, but this has to be done by someone who implements these types.

    2) Do not provide default one and specialize for basic types

    template <typename T> struct Arg;
    template <> struct Arg <int> ...
    template <> struct Arg <float> ...
    // and so on...
    template <typename T> struct Arg <Wrap<T> > ...
    

    It’s not ideal too (depends on how many “basic types” you expect to use)

    3) Use IsDerivedFrom trick

    template<typename T> struct Wrap { typedef T type; };
    class IntArg: public Wrap<int> {};
    class FloatArg: public Wrap<float> {};
    
    template<typename D>
    class IsDerivedFromWrap
    {
        class No { };
        class Yes { No no[3]; }; 
    
        template <typename T>
        static Yes Test( Wrap<T>* ); // not defined
        static No Test( ... ); // not defined 
    
    public:
        enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) }; 
    };
    
    
    template<typename T, bool DerivedFromWrap> struct ArgDerived;
    
    template<typename T> struct ArgDerived<T, false>
    {
        static inline const T*   Ptr (const T* arg)
        {
            std::cout << "Arg::Ptr" << std::endl;
            return arg;
        }
    };
    
    template<typename T> struct ArgDerived<T, true>
    {
        static inline const typename T::type* Ptr (const T* arg)
        {
            std::cout << "Arg<Wrap>::Ptr" << std::endl;
            return 0;
        }
    };
    
    template<typename T> struct Arg : public ArgDerived<T, IsDerivedFromWrap<T>::Is> {};
    
    template<typename T>
    void UseArg(T argument)
    {
        Arg<T>::Ptr(&argument);
    };
    
    void test()
    {
        UseArg(5);
        UseArg(IntArg());
        UseArg(FloatArg());
    }
    

    Calling test() outputs (as I understand that’s your goal):

    Arg::Size 
    Arg<Wrap>::Size
    Arg<Wrap>::Size
    

    Extending it to work with more types like Wrap is possible, but messy too, but it does the trick – you don’t need to do a bunch of specializations.

    One thing worth mentioning is that in my code ArgDerived is specialized with
    IntArg instead of Wrap<int>, so calling sizeof(T) in ArgDerived<T, true> returns size of IntArg instead of Wrap<int>, but you can change it to sizeof(Wrap<T::type>) if that was your intention.

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

Sidebar

Related Questions

No related questions found

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.