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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:27:56+00:00 2026-05-13T20:27:56+00:00

If I have a little peice o’ code as such… template <typename _T> class

  • 0

If I have a little peice o’ code as such…

template <typename _T>
class Foo
{
public:
    typedef const T& ParamType;
    void DoStuff(ParamType thingy);
};

This can be non-optimal if sizeof(_T) <= sizeof(_T*).

Therefore, I want to have a conditional typedef. If the size of _T is less than or equal to that of a pointer, just pass it in by value. Otherwise, pass it by const reference. Is this possible? I hear all this stuff about templates being turing complete but this is hurting my head.

  • 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-13T20:27:56+00:00Added an answer on May 13, 2026 at 8:27 pm

    Quite easy to achieve using partial template specialization.

    template< typename _T, bool _ByVal >
    struct FooBase {
      typedef const _T& ParamType;
    };
    
    template< typename _T >
    struct FooBase< _T, true > {
      typedef const _T ParamType;
    };
    
    template< typename _T, bool _ByVal = sizeof(_T) <= sizeof(void*) >
    class Foo : public FooBase< _T, _ByVal > {
      typedef typename FooBase< _T, _ByVal >::ParamType ParamType;
      void DoStuff(ParamType thingy);
    };
    

    EDIT As per Jeff’s sol’n one should indeed compare sizeof(_T) and sizeof(_T&) but I kept the original <= void* requirement.

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

Sidebar

Related Questions

I have a little splash window: public partial class Splash : Form { bool
I have this little function here: public Activity getRootActivity() { Activity a = this;
I have some ideas for web apps. Sadly, I have very little knowledge of
I have a bookmarklet that appends the body to bring up a little litebox.
I have a little web app that uses the Facebook like widget. This page
i have just started programming for android and i have a little question i
I've finished my C# application, but I have a little problem: When I try
Is it possible to filter out subsets of the data that have small numbers
I have a page using Paypal Payments Standard with buy one now buttons on
I am implementing a sort and stream compaction algorithms in CUDA C. However I

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.