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

The Archive Base Latest Questions

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

I would like to provide a templated function, that varies its implementation (->specialization) according

  • 0

I would like to provide a templated function, that varies its implementation (->specialization) according to the sizeof the template type.

Something similar to this (omitted typecasts), but without the if/elseif:

template<class T>
T byteswap(T & swapIt)
{
    if(sizeof(T) == 2)
    {
        return _byteswap_ushort (swapIt);
    }
    else if(sizeof(T) == 4)
    {
        return _byteswap_ulong(swapIt);
    }
    else if(sizeof(T) == 8)
    {
        return _byteswap_uint64(swapIt);
    }
            throw std::exception();
}

I know there are many roads to reach my goal, but since I try to learn about SFINAE and type traits I’m particularly interested in solutions using those techniques to decide at compile time which specialization to choose and which calls are not admitted.

Perhaps implementing a class trait is_4ByteLong and using boost::enable_if…

I have to admit, I’m stuck right now, so I thank you for any help or advice

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

    You don’t need SFINAE or type traits. Vanilla template specialization is enough. Of course it must be specialized on structs as C++(98) doesn’t support function template partial specialization.

    template <typename T, size_t n>
    struct ByteswapImpl
    /*
    {
      T operator()(T& swapIt) const { throw std::exception(); }
    }
    */    // remove the comments if you need run-time error instead of compile-time error.
    ;
    
    template <typename T>
    struct ByteswapImpl<T, 2> {
      T operator()(T& swapIt) const { return _byteswap_ushort (swapIt); }
    };
    
    // ...
    
    template <typename T>
    T byteswap(T& swapIt) { return ByteswapImpl<T, sizeof(T)>()(swapIt); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to provide a PHP hanging request that will stay open until
I would like to know how to provide a formatting capabilities that enable the
I would like to make a class that has a function that saves the
I have a smart pointer type, and would like to construct an object that
I want to provide a templated function that converts most basic types to string.
If possible, I would like to have a function in a class that returns
I have a set of C++ libraries and now I would like provide a
I would like to provide the same content inside 2 different base files. So,
I would like to provide a facade class to the existing AmazonS3Client (say MyAmazonS3Client),
I would like to provide localization for my website in Azure. I went with

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.