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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:29:36+00:00 2026-05-26T18:29:36+00:00

I have created a type list. I then create a class using a template

  • 0

I have created a type list. I then create a class using a template passing the type list. When I call the print function of the class with a some types not specified they are casted. How can I enforce the exact type at compile time? So if I use an unlisted type I get a compiler error.
Thanks.

template <class T, class U>
struct Typelist
{
   typedef T Head;
   typedef U Tail;
};


class NullType
{
};

typedef Typelist<int,Typelist<float,Typelist<char*,NullType> > > UsableTypes;

template<class T>
class MyClass
{
    public:
    void print(T::Head _Value) { std::cout << _Value; }
    void print(T::Tail::Head _Value) { std::cout << _Value; }
    void print(T::Tail::Tail::Head _Value) { std::cout << _Value; }
    private:
};


MyClass<UsableTypes> testclass;

void TestMyClass()
{
    int int_val = 100000;
    float flt_val = 0.1f;
    char* char_val = "Hi";
    short short_val = 10;
    std::string str_val = "Hello";

    testclass.print( int_val ); // OK  8-)
    std::cout << endl;
    testclass.print( flt_val ); // OK  8-)
    std::cout << endl;
    testclass.print( char_val ); // OK  8-)
    std::cout << endl;
    testclass.print( short_val); // this compiles OK and works ???  8-(
    std::cout << endl;
    testclass.print( str_val ); // compile error  8-)
    std::cout << endl;
}

@Kerrek SB: Hi I thought it was going to help me with my next step, which was creating the print function depending on the t_list contents, Types and amounts of types. But I’m struggling to separate compile time processing and runtime processing. What I am trying to do is create a print function for each type in the list. So if the list has two types, two print functions will be created and if there are five types then five print functions will be created one for each type.
When I do this:

 typedef Typelist<int,Typelist<float,Typelist<char*,NullType> > > UsableTypes;

 MyClass<UsableTypes> newclass

Does this create three instance of MyClass one for each type in the list or does it create one instance and I have to create a print function for each type?
I feel I almost have all the blocks in my mind but just can’t fit them together. Any help you can offer would be gratefully received. Thanks.

  • 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-26T18:29:36+00:00Added an answer on May 26, 2026 at 6:29 pm

    Add a private function template

    template<typename T> void print(T);
    

    which doesn’t need an implementation. This should catch all types for which no explicit print exists, and since it is private, it will give an error message.

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

Sidebar

Related Questions

HI i have created a content type call mycontent it has 2 fields Label:
I have created some simple charts (of type FastLine) with MSChart and update them
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have a project that is using a generic list of a custom class.
I have created the following type using implicit type construction: open System type Matrix(sourceMatrix:double[,])
I have created a library which reads the app.config file and gets the type
i created a FTS by this article i have a column with type of
I have my own type: CREATE TYPE MyType AS TABLE ( foo INT )
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT
I have to create a dialog based application, instead of old CFormView type of

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.