最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

compilation - Create C++ macro which defines a compiler variable and calls method - Stack Overflow

programmeradmin4浏览0评论

I am trying to create a compiler macro in C++ that will define a (compiler) variable and then call a C++ method. For example, if I have this in my C++ code:

TL_CLI_ADD_EXIT_CODE(123,"SOMENAME","Description of code")

I want the compiler to expand it to:

#define EXITCODE_SOMENAME   123
addExitCode(123,"SOMENAME","Description of code");

The closest I've gotten for code is:

// Macro to concatenate EXITCODE_ prefix with the number
#define TL_CLI_CONCAT_EXITCODE(number) EXITCODE_##number

// Macro to create a new exit code definition
#define TL_CLI_ADD_EXIT_CODE(c,n,d) \
  #define TL_CLI_CONCAT_EXITCODE(n) c \
  addExitCode(c,\"n\",d);

Is what I'm trying to do even possible? I seem to cycle between various errors including "'#' is not followed by a macro parameter". Can someone advise what is wrong?

I am trying to create a compiler macro in C++ that will define a (compiler) variable and then call a C++ method. For example, if I have this in my C++ code:

TL_CLI_ADD_EXIT_CODE(123,"SOMENAME","Description of code")

I want the compiler to expand it to:

#define EXITCODE_SOMENAME   123
addExitCode(123,"SOMENAME","Description of code");

The closest I've gotten for code is:

// Macro to concatenate EXITCODE_ prefix with the number
#define TL_CLI_CONCAT_EXITCODE(number) EXITCODE_##number

// Macro to create a new exit code definition
#define TL_CLI_ADD_EXIT_CODE(c,n,d) \
  #define TL_CLI_CONCAT_EXITCODE(n) c \
  addExitCode(c,\"n\",d);

Is what I'm trying to do even possible? I seem to cycle between various errors including "'#' is not followed by a macro parameter". Can someone advise what is wrong?

Share Improve this question asked Jan 18 at 16:56 TSGTSG 4,61710 gold badges71 silver badges140 bronze badges 7
  • 1 "I am trying to create a compiler macro" -- Why? (I'm not asking for your goal, but why must it be an evil macro?) – JaMiT Commented Jan 18 at 17:01
  • If you insist on a solution that follows the strategy you outlined: A: Define macro within another macro ("It's not possible to define a macro inside a macro.") and A: Expand define macro with include macro (quoting the standard) – JaMiT Commented Jan 18 at 17:08
  • If you can use C++17 or newer then #define EXITCODE_SOMENAME 123 should be replaced with inline constexpr int EXITCODE_SOMENAME = 123; – NathanOliver Commented Jan 18 at 17:09
  • The team I worked on about 30 years ago solved this problem by having a Python script that generated header file of #define, which that was then #included by the C++ code. Can you use Python (or some other scripting langauge) to create the compiler macro #define in a generated header file for your project? – Eljay Commented Jan 18 at 17:25
  • Well... I come from the C world but if there's a better way, can you show an example in an answer? So long as I can use the EXITCODE_COMENAME later (as an int) in other classes. (Which is why I was using a compiler definition) – TSG Commented Jan 18 at 17:54
 |  Show 2 more comments

1 Answer 1

Reset to default 0

This is C++, try to avoid MACROs when you can. Often there is some kind of constexpr approach possible, like this :

#include <string_view>
#include <vector>
#include <iostream>
#include <format>

struct ExitCode
{
    int code;
    std::string_view name;
    std::string_view description;

    // implicit conversion to int (for interop with other API's)
    operator int() const noexcept
    {
        return code;
    }
};

std::ostream& operator<<(std::ostream& os, const ExitCode& err)
{
    os << std::format("Error : {}, description = {}", err.name, err.description);
    return os;
}

//-----------------------------------------------------------------------------

static constexpr ExitCode EXITCODE_SOMENAME{123,"SOMENAME","Description of code"};

// return a ref, all data will be put in by compiler (no dynamic mem alloc)
const ExitCode& SomeFunction()
{
    return EXITCODE_SOMENAME;
}

int main()
{
    auto& retval = SomeFunction();
    std::cout << retval << "\n";
    return retval; // implicit conversion to int here
}
发布评论

评论列表(0)

  1. 暂无评论