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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:28:55+00:00 2026-05-23T06:28:55+00:00

as a follow up to this question, how can the code be changed so

  • 0

as a follow up to this question, how can the code be changed so that i can use it in the constructor of a class? im writing a new class where the input needs to be an number of some kind, but nothing else. the code, however, is like declaring the type in front of a function. since constructors dont exactly have types, i need it to not declare a type for the function itself

my new class:

class C{
     public:
         C();
         C(T value);// specifically looking for this


         T f(T value); // what the code currently does

};

the code in the link creates a function that [accepts and] returns an integer type T. i need it to not return anything at all, so that it can be used with a constructor

  • 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-23T06:28:56+00:00Added an answer on May 23, 2026 at 6:28 am

    I think you want to restrict types for constructor template. If so, then you can do this:

    #include <type_traits>
    //#include <tr1/type_traits> // for C++03, use std::tr1::
    
    class C
    {
      public:
    
         template<typename T>
         C(T value, typename enable_if<std::is_arithmetic<T>::value,T>::type *p=0)
         {
    
         }
    };
    

    This constructor template can accept only those T for which is_arithmetic<T>::value is true. The implementation of enable_if is exactly same, as given in the other answer.


    Alternatively, or if you don’t have type_traits, then you can use typelist along with enable_if. I think this is a better solution, as you can specifically define the supported typelist.

    typedef typelist<int> t1;
    typedef typelist<short, t1> t2;
    typedef typelist<char, t2> t3;
    typedef typelist<unsigned char, t3> t4;
    //and so on
    
    typedef t4 supported_types;//supported_types: int, short, char, unsigned char
    
    class C
    {
      public:
    
         template<typename T>
         C(T value, typename enable_if<exits<T,supported_types>::value,T>::type *p=0)
         {
    
         }
    };
    

    This constructor template can accept only those T for which exists<T,supported_types>::value is true. The exists metafunction checks whether T is existing in the typelist supported_types or no. You can add more types to this typelist.

    And the implementation of typelist, and exists is here (see my solution):

    • Restrict parameter type in template instantiation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up question to that at Can I create a HTML
This is a follow-on question from the one I asked here . Can constraints
Follow up to this question . I have the following code: string[] names =
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
[a follow up to this question ] class A { public: A() {cout<<A Construction
As a follow up to this question , I have a parent entity that
Follow up to this question about GNU make : I've got a directory, flac
This question is a follow on from this one ... I am binding to
This question is kind of a follow up to this question I asked a
This is a follow up of this question: Puzzle - How to 'colorify' part

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.