0

I try to expand my knowledge about macros and their usage.

I have a specific problem which i bumped.

Here's my situation

  • I have a class named RudyObject
  • This class has a get function for both member(GetRudyObjectID()) and static(GetStaticRudyObjectID()).

Here's my problem

I plan to create macro which adapts itself to given situation.

Here's the scenario i'd like to solve

  • TYPEOF(variable value type) should expand as variable.GetRudyObjectID()
  • TYPEOF(variable pointer) should expand as variable->GetRudyObjectID()
  • TYPEOF(type) should expand astype::GetStaticRudyObjectID()

Do you guys have any solution,tips or directios for this situation.

Roveldo
  • 47
  • 4
  • 1
    Macros work by pure text substitution, they have no information about variables or types – UnholySheep Sep 01 '21 at 20:46
  • Tip: don't use a macro. Never use a macro. If you think you need a macro, try using a language feature like constexpr (or consteval) functions instead. See also [Why are preprocessor macros evil and what are the alternatives?](https://stackoverflow.com/q/14041453/11082165) – Brian61354270 Sep 01 '21 at 20:47
  • 1
    There are a lot of features in C++ that you can use besides macros to get this type of functionality. For instance, simply overloading a function for values and pointers covers bullets 1 and 2. Could we see a real-ish example of what you are actually trying to achieve? – NathanOliver Sep 01 '21 at 20:49
  • And what if there's variable with the same name as some class? – Alexey S. Larionov Sep 01 '21 at 20:50
  • You better always provide a snippet of code when asking a question. – Karen Baghdasaryan Sep 01 '21 at 21:02
  • If it even ends up being possible to add 3 into the mix without static reflection, I can guarantee you it would not be worth it. There are only a few places in the language where you can use both type names and non-type names in the same syntactical fragment, and adapting any of those to create this macro would be an abomination. God help you if anyone ever slightly misuses it or the macro doesn't account for a special case, because it will ruin someone's day. – chris Sep 01 '21 at 21:26
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 05 '21 at 00:16

1 Answers1

1

The first two you can easily get via a template with a specialization for pointers (via if constexpr) and the third is another overload only taking a template parameter:

#include <string>
#include <iostream>

template <typename T>
auto type_of(const T& t){
    if constexpr (std::is_pointer_v<T>) {
        return std::string{"a pointer \n"};
    }
    else { 
        return std::string{"not a pointer \n"};
    }
}

template <typename T>
auto type_of() {
    return std::string{"type_of without parameter \n"};
}


int main()
{
    int x;
    std::cout << type_of(x);
    std::cout << type_of(&x);
    std::cout << type_of<int>();
    return 0;
}

Output:

not a pointer 
a pointer 
type_of without parameter 

I advise you to not use macros for this. They have their place in automatic code generation, but using them to save a bit of typing will typically lead to hard to read, maintain and debug code when other C++ features can replace them easily without all those downsides. Moreover, macros are expanded before the code is compiled, ie the preprocessor does not know about types, pointers or variables. Macros are only about replacing tokens.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185