F
F
floppa3222022-02-04 17:54:22
C++ / C#
floppa322, 2022-02-04 17:54:22

Is it possible to declare a class member variable using metaprogramming?

Code example:

template <typename T, bool someCondition>
class Example
{

//    #ifdef SOME_CONDITION
//    int x;  
//    #endif

    // ↓ ↓ ↓  Ниже представлен псевдокод-эквивалент закомментированому коду выше: ↓ ↓ ↓ 
    if constexpr (someCondition)
    {
        int x;  
    }
    
  /* Some code... */

};

Is there an equivalent to a piece of code that is commented out (using a macro) but with the help of metaprogramming?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cunion, 2022-02-04
@Lite_stream

template <bool X>
struct test
{};

template <>
struct test<true>
{
  int a;
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question