1
#define object object() // object -> object()
#define none object     // none -> object -> object()
#define lambda (std::function<****>)[]()  // HOW?!, lambda -> (std::function<object(object, const char*)>)[]()

The problem:

#define object object() // object -> object() changes object to object().

What i want is to make :

lambda -> (std::function<object(object, const char*)>)[]().

But due to my previous macros object turns to object().So i have :

(std::function<object()(object(), const char*)>)[]()

Is there a smarter way of doing this, maybe using #ifdef or something? I never used preprocessor macros before, so maybe am doing everything wrong. It seems it doesn't matter which one I #define first.

ANSWER:: I changed the class name from object to Object

#define object Object() // object -> Object()
#define none object     // none -> object -> Object()
#define lambda (std::function<Object(Object, const char*)>)[]() 
// now pre possessor has nothing to change and everything works fine
Lampros
  • 321
  • 3
  • 10
  • 11
    In general : Yes don't use macro's and if you must specialize code use function and class templates. However its not at all clear to me what you are trying to do, so maybe you could explain that. Before you do that have a look here though : https://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives – Pepijn Kramer Dec 31 '21 at 14:28
  • 6
    Assuming this is some kind of lambda-defining convenience macro, why forcefully cast lambdas to `std::function`? The users that want a `std::function` can create one themselves. – HolyBlackCat Dec 31 '21 at 14:30
  • 2
    Be especially leery of object-like macros in a context like this. – Jonathan Leffler Dec 31 '21 at 14:31
  • 1
    @HolyBlackCat [I cast to solve an overloading problem](https://stackoverflow.com/questions/70454074/function-taking-lambda-argument-overloads-to-bool) – Lampros Dec 31 '21 at 14:52
  • @PepijnKramer We are making a specialized purpose language in c++ , I need to hide some stuff ( its for a project). – Lampros Dec 31 '21 at 14:54
  • 1
    A specialized language that sets of some alarm bells. I would say it is okay for educational purposes but I wouldn't do it (anymore) for a commercial project. Such a choice usually bites back, lots of maintenance to develop the language, lots of support to teach people the language. Have you considered writing an API that can be used by existing languages (like python, or C#)? – Pepijn Kramer Dec 31 '21 at 15:03
  • 2
    @Lampros Just know that macros can make for bugs which are very hard to find and fix, as well as for very cryptic compiler errors. If you have any choice in the matter, don't use macros. – hyde Dec 31 '21 at 15:04
  • Sadly I don't, could you at least give me some advice on how to achieve what i want? Thanks alot! – Lampros Dec 31 '21 at 15:44
  • 1
    Could you tell, what you generally try to achieve and what macro command should be translated to what code? – Sebastian Dec 31 '21 at 15:52
  • `lambda` -> `(std::function)[]()` But due to my previews macros `object` turns to `object()` – Lampros Dec 31 '21 at 15:55
  • 1
    `Is there a smarter way of doing this?` Doing "what" exactly? There is literally nothing happening in the code. There is no smarter way of defining three macros with those values. To define three macros do just that, as your code does. Can you specify "what" are you doing? (And how to measure "smartness" property of "doing" it?) What is the end goal? If you tried to achieve that end goal or use these macros - in what way did you use those macros and what error messages are you getting? – KamilCuk Dec 31 '21 at 15:55
  • @KamilCuk See the question again, i tried to clarify some things. – Lampros Dec 31 '21 at 16:06
  • 2
    Even if others already told it: Stop that kind of macro hacking. From my point of view it is much easier and clearer to define a set of thin helper/creater functions which do the job without any mystery, but type safe and with full syntax check. As I also have no idea what you really want to achieve, it might be an XY problem? – Klaus Dec 31 '21 at 16:16
  • Anyway, as for `Is there a smarter way of doing this` if you want to do a single string replacement, do not use C preprocessor at all. Really just use `sed` for a string replacement. Or other templating system, C preprocessor lakcs many features. Use M4 , or Jinja2 or php. – KamilCuk Dec 31 '21 at 17:57
  • I do not understand, so just remove the line `#define object object()`, or rename it. – KamilCuk Dec 31 '21 at 18:02
  • How about having an C++ alias name (e.g. reference) for object. So that the macro resolution of lambda can generate another name than object, which always automatically gets parantheses? So mix preprocessor and language 'tricks' – Sebastian Dec 31 '21 at 18:23
  • 1
    Assuming `object()` is some kind of function, why must it have the same name as the macro? Just rename the function? It might be possible to somehow prevent the macro expansion and allow `object(...)` to exist, but it will be unstable in the sense that if you pass it to another macro as an argument, it may or may not be expanded again, causing a error (since you'd be passing arguments to parameter-less macro). – HolyBlackCat Dec 31 '21 at 19:11

1 Answers1

4

Short answer: every line of what you propose is bad practice, up to and including casting lambdas to std::function – the latter being a premature pessimization and almost never necessary nor useful.

Just write C++. It’ll be clearer to everyone.

Your question is a classic XY problem: you’re proposing a solution without stating what the problem is. The problem likely has a way easier solution or is not a problem to begin with.

You’ve stated the “problem” but that’s not it: I have no idea why you need those macros for anything. So those are not your problem. Show what sort of code you want written, and how those macros would supposedly help.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313