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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:07:12+00:00 2026-06-06T11:07:12+00:00

I’m creating a DNS name resolver. While parsing the returned packet I read the

  • 0

I’m creating a DNS name resolver. While parsing the returned packet I read the RR type then pass it through switch to initialize a derived type of class Record. Record types is a large enum type. I map the enum to a struct using template specialization.

template < QueryType n >
struct Struct;

template <> struct Struct< DNS_Q_A > { typedef A Type; };
template <> struct Struct< DNS_Q_CNAME > { typedef CNAME Type; };

template < QueryType n > struct Struct { typedef UNKNOWN Type; };

Currently I have 4 switch statements which all do a very similar thing. Namely call operator new, placement new, for both copy and move c’tors. This is a lot of code to maintain. I would like to have just 1 switch statement where I could pass in some type of object containing the function to perform, return type and n number of parameters.

The switch statements are like this:

switch ( nType )
{
case DNS_Q_A:
    pInstance = new ( &Container ) Struct< DNS_Q_A >::Type( dynamic_cast< const Struct< DNS_Q_A >::Type& >( Other ) );
    break;
case DNS_Q_CNAME:
    pInstance = new ( &Container ) Struct< DNS_Q_CNAME >::Type( dynamic_cast< const Struct< DNS_Q_CNAME >::Type& >( Other ) );
    break;
}

As you can see, each case is identical other than the reliance on the struct type. This screams out ‘template’ to me but I can’t figure out how to pass in an object.

Am I limited to coding 4 switch’s or is there a way? Please don’t quote “Boost”, this code has to be independent of any other libraries.

Solution: (Thanks to Jan Hudec)

template< template < class > class Action >
typename Action< Struct< DNS_Q_A >::Type >::result_type
    CreateRecord ( 
        unsigned n,
        typename Action< Struct< DNS_Q_A >::Type >::first_argument_type arg1,
        typename Action< Struct< DNS_Q_A >::Type >::second_argument_type arg2 )
{
    typedef typename typename Action< Struct< DNS_Q_A >::Type >::result_type ReturnType;
    switch ( n )
    {
    case DNS_Q_A:       return static_cast< ReturnType >( Action< Struct< DNS_Q_A >::Type >()( arg1, arg2 ) );
    case DNS_Q_NS:      return static_cast< ReturnType >( Action< Struct< DNS_Q_NS >::Type >()( arg1, arg2 ) );
    /*...*/
    }
}

with Action struct defined as:

template < typename T >
struct Copy : std::binary_function< storage_type&, const Record&, Record* >
{
    Record* operator() ( storage_type& Storage, const Record& Obj )
    {
        return new ( &Storage ) T( dynamic_cast< const T& >( Obj ) );
    }
};
template < typename T >
struct Move : std::binary_function< storage_type&, Record&&, Record* >
{
    Record* operator() ( storage_type& Storage, const Record& Obj )
    {
        return new ( &Storage ) T( dynamic_cast< const T& >( std::move( Obj ) ) );
    }
};

And Switch statements replaced with:

pInstance = CreateRecord< Copy >( nType, Container, Other );
  • 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-06T11:07:13+00:00Added an answer on June 6, 2026 at 11:07 am

    You can create a function template that will take functor template to execute. Without C++11 variadic arguments all the functors need the same number of arguments, so you will have to either pack them in a structure or pass NULLs when they are irrelevant. With variadic templates (I haven’t use them yet, so I don’t remember exact syntax and won’t write them here) you can easily have different arguments in each form.

    The idea is like (off the top of my head, so there might be some typos):

    template <template <typename T> class F>
    F<Struct<DNS_Q_A>::Type>::return_type RecordCall(RecordType nType, const F<Struct<DNS_Q_A>::Type>::argument_type &arg)
    {
        switch(nType)
        {
            case DNS_Q_A:
                return F<Struct<DNSK_Q_A>::Type>()(arg);
            case DNS_Q_CNAME:
                return F<Struct<DNSK_Q_CNAME>::Type>()(arg);
            // ...
         }
    }
    

    Now you write the individual functions like:

    template <typename T>
    struct Clone : std::unary_function<BaseType *, const BaseType *>
    {
        BaseType *operator()(const BaseType *source)
        {
            return new<T>(dynamic_cast<const BaseType &>(*source));
        }
    }
    

    and combine together:

    target = RecordCall<Clone>(source->GetType(), source);
    

    (and wrap in another function, that will insert the getter and/or pack arguments for the multi-argument forms like placement copy construction)

    Though for copying the common way to do this is to have virtual Clone member method. But that won’t work for construction.

    Edit: Note, that I defined the return and argument types within the inner template as typedefs (using the standard unary_function helper), but they can alternatively be passed as separate template arguments, especially if the use of RecordCall is going to be wrapped in another function. Perhaps the return type does not even have to be a template parameter as it’s going to be Record * for all the cases mentioned in the question.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an XML file, the creators of it stuck in a bunch social
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.