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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:27:05+00:00 2026-06-01T11:27:05+00:00

Let’s assume, that we have an enumered type: enum DataType { INT, DOUBLE };

  • 0

Let’s assume, that we have an enumered type:

enum DataType { INT, DOUBLE };

And a type mapper:

template<DataType T>
struct TypeTraits {}; 

template<>
struct TypeTraits<INT> { typedef int T; };

template<>
struct TypeTraits<DOUBLE> { typedef double T; };

And a few templates which represents operations (don’t bother about ugly void pointers, and C-like typecasts):

struct Operation {
  DataType rettype;

  Operation(DataType rettype) : rettype(rettype);
  virtual void* compute();
};

template<DataType RetType>
class Constant : public Operation {
  typedef typename TypeTraits<RetType>::T RType;
  RType val;

  Constant(RType val) : val(val), Operation(RetType) {}; 
  virtual void* compute(){ return &val; }
};  

template<DataType T1, DataType T2, DataType RetType>
class Add : public Operation {
  typedef typename TypeTraits<RetType>::T1 T1Type;
  typedef typename TypeTraits<RetType>::T2 T2Type;
  typedef typename TypeTraits<RetType>::RetType RType;
  RType val;

  Operation *c1, *c2;

  Add(Operation *c1, Operation *c2) : c1(c1), c2(c2), Operation(RetType) {}; 

  virtual void* compute(){
    T1Type *a = (T1Type *)c1->compute();
    T2Type *b = (T2Type *)c2->compute();
    val = *a + *b; 
    return &val;
  }   
};  

And a abstract tree representation:

class AbstractNode {
  enum Type { ADD, INT_CONSTANT, DOUBLE_CONSTANT };

  Type type;
  int intval;
  double doubleval;
  child1 *AbstractNode;
  child2 *AbstractNode;
}

We’re reading a serialized abstract tree from input in order to translate it into an operation tree, and then – compute a result.

We want to write something like:

algebrator(Operation *op){
  if(op->type == AbstractNode::INT_CONSTANT)
    return new Constant<INT>(op->intval);
  else if(op->type == AbstractNode::DOUBLE_CONSTANT)
    return new Constant<DOUBLE>(op->doubleval);
  else {
    Operation *c1 = algebrator(op->child1),
              *c2 = algebrator(op->child2);
    DataType rettype = add_types_resolver(c1->rettype, c2->rettype);
    return new Add<c1->rettype, c2->rettype, rettype>(c1, c2);
  }
}

where add_types_resolver is something which specifies the return type of add operation based on operation arguments types.

And we fail of course and compiler will hit us into the faces. We can’t use a variable as a template variable! It’s because all information needed to instantiate template must be available during the compliation!


And now – the question.

Is there any other solution than writing a plenty of if-else, or switch-case statements? Can’t we ask a compiler in any way to expand all cases during compilation? Template is parametrized by the enum, so we have a guarantee, that such process is finite.

And please – don’t write responses like “I think the whole example is messed up”. I just want to know if there’s a way to feed the template with variable, knowing it’s from a finite, small set.

The whole thing may seem like an overkill, but I’m really curious how can I instantiate classes in such unusual situations.

  • 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-01T11:27:06+00:00Added an answer on June 1, 2026 at 11:27 am

    Quick’n’dirty solution with macros:

    column_type.cc:

    enum ColumnType {
      INT = 1,
      DOUBLE = 2,
      BOOL = 3
    };
    

    typed_call_test.cc (usage example):

    #include <iostream>
    #include "column_type.cc"
    #include "typed_call.cc"
    
    template <ColumnType T>
    void PrintType() {
      ::std::cout << T <<::std::endl;
    }
    
    int main() {
      ColumnType type = INT;
      // this won't compile:
      // PrintType<type>();                  
      // and instead of writing this:
      switch (type) {                  
        case INT:                  
          PrintType<INT>();                  
          break;                  
        case DOUBLE:                  
          PrintType<DOUBLE>();                  
          break;                  
        case BOOL:                  
          PrintType<BOOL>();                  
          break;                  
      }                  
      // now you can write this:
      TYPED_CALL(PrintType, type, );
    
      return 0;
    }
    

    typed_call.cc (“library”):

    // Requirements:
    // |function| return type must be void
    //
    // Usage:
    //
    // having for instance such |type| variable:
    //   ColumnType type = INT;
    // and such |foo| function definition:
    //   template <ColumnType T>
    //   void foo(t1 arg1, t2 arg2) {
    //     …
    //   }
    //
    // instead of writing (won't compile):
    //   foo<type>(arg1, arg2);                  
    // write this:
    //   TYPED_CALL(foo, type, arg1, arg2);
    //
    //
    // for foo with 0 arguments write this:
    //   TYPED_CALL(foo, type, );
    //
    #define TYPED_CALL(function, type, args...) {                        \
      switch (type) {                                                    \
        case INT:                                                        \
          function<INT>(args);                                           \
          break;                                                         \
        case DOUBLE:                                                     \
          function<DOUBLE>(args);                                        \
          break;                                                         \
        case BOOL:                                                       \
          function<BOOL>(args);                                          \
          break;                                                         \
      }                                                                  \
    }
    
    #define BASE_TYPED_CALL(function, type, args...) {                   \
      switch (type) {                                                    \
        case INT:                                                        \
          function<int>(args);                                           \
          break;                                                         \
        case DOUBLE:                                                     \
          function<double>(args);                                        \
          break;                                                         \
        case BOOL:                                                       \
          function<bool>(args);                                          \
          break;                                                         \
      }                                                                  \
    }
    

    To take this solution “level up” you can replace macro with a function (still containing similiar switch construct). But probably you would like to pass a functor (object with () operator) as a parameter of this function instead of an ordinary function like in this macro. Btw: this is exactly how they do it in Google.


    1st sidenote: hi to my classmate from the Columnar and Distributed DataWarehouses course on University of Warsaw! This course is generating a lot of mind-bending C++ template questions 🙂

    2nd sidenote: here is how my equivalent of Typetraits looks like:

    template <ColumnType T>
    struct EnumToBuiltin {
    };
    
    template <>
    struct EnumToBuiltin<INT> {
      typedef int type;
    };
    template <>
    struct EnumToBuiltin<DOUBLE> {
      typedef double type;
    };
    template <>
    struct EnumToBuiltin<BOOL> {
      typedef bool type;
    };
    
    
    template <typename T>
    struct BuiltinToEnum {
    };
    
    template <>
    struct BuiltinToEnum<int> {
      static const ColumnType type = INT;
    };
    template <>
    struct BuiltinToEnum<double> {
      static const ColumnType type = DOUBLE;
    };
    template <>
    struct BuiltinToEnum<bool> {
      static const ColumnType type = BOOL;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's assume that we are building a high traffic site that will be used
Let's say I have multiple requirements for a password. The first is that the
Let's assume that a user votes for some movies in a scale of 1
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a model that handles recipes, and I want to
Let's suppose we have: Class Foo{ int x,y; int setFoo(); } int Foo::setFoo(){ return
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's imagine I have a Java class of the type: public class MyClass {
Let's say that I have a set of relations that looks like this: relations

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.