C++ 是否可以使作为默认参数的宏在调用站点上展开?

C++ 是否可以使作为默认参数的宏在调用站点上展开?,c++,c++11,macros,default-arguments,std-source-location,C++,C++11,Macros,Default Arguments,Std Source Location,如果我可以保存键入内容,而不为每个函数创建宏,则会更方便,如下所示: #include <stdio.h> void print(int a = __LINE__){printf("hello %d\n", a);} void main(){ print(); print(); print(); print(); } #define S1(x) #x //voodoo to concat __FILE__ and __LINE__ #define S2(x) S

如果我可以保存键入内容,而不为每个函数创建宏,则会更方便,如下所示:

#include <stdio.h>

void print(int a = __LINE__){printf("hello %d\n", a);}

void main(){
  print();
  print();
  print();
  print();
}
#define S1(x) #x //voodoo to concat __FILE__ and __LINE__
#define S2(x) S1(x)
#define LOCATION __FILE__ S2(__LINE__)

do_stuff1(arguments, LOCATION)
do_stuff2(arguments, LOCATION)

因此,我认为默认参数会起作用。有什么方法可以实现这一点吗?

您似乎正在寻找,这将是未来标准中的
std::source\u location

#define do_stuff1(do_stuff1_imp(arguments, LOCATION))
#define do_stuff2(do_stuff2_imp(arguments, LOCATION))
#包括
无效打印(标准::实验::源位置常数和位置
=标准::实验::源位置()
{
printf(“hello%s:%d\n”,location.file_name(),location.line());
}

您似乎正在寻找,这将是未来标准中的
std::source\u位置

#define do_stuff1(do_stuff1_imp(arguments, LOCATION))
#define do_stuff2(do_stuff2_imp(arguments, LOCATION))
#包括
无效打印(标准::实验::源位置常数和位置
=标准::实验::源位置()
{
printf(“hello%s:%d\n”,location.file_name(),location.line());
}
本例中的
\uuuu行\uuuu
宏扩展为3

当然可以,因为函数声明是在第3行完成的,预处理器会从那里展开它

另一个宏而不是函数声明可以很好地解决这个问题(摘自:

请参见一个工作示例

本例中的
\uuuu行\uuuu
宏扩展为3

当然可以,因为函数声明是在第3行完成的,预处理器会从那里展开它

另一个宏而不是函数声明可以很好地解决这个问题(摘自:


请参阅一个工作示例。

您可以编写一个宏,将
\uuuuuuuuuuuu文件和
\uuuuuuuuuuu行插入参数列表中:

#define print() printf("hello %d\n", __LINE__)
只需多做一点工作,您就可以在呼叫站点上让它看起来更漂亮:

// Note: this doesn't work with a 0-argument function. Supporting 0-arg functions
// is much harder to do; I'd recommend just having a separate macro like as follows
#define LOCATED(fn, ...) fn(__VA_ARGS__, __FILE__, __LINE__)

#define LOCATED0(fn) fn(__FILE__, __LINE__)

void print(char const* file_name, int line)
{
    printf("hello %s:%d\n", file_name, line);
}

int do_something(int value, char const* file_name, int line);

int main() {
    LOCATED0(print);
    LOCATED(do_something, 42);
}

您可以编写一个宏,将
\uuuu文件和
\uuuu行插入参数列表中:

#define print() printf("hello %d\n", __LINE__)
只需多做一点工作,您就可以在呼叫站点上让它看起来更漂亮:

// Note: this doesn't work with a 0-argument function. Supporting 0-arg functions
// is much harder to do; I'd recommend just having a separate macro like as follows
#define LOCATED(fn, ...) fn(__VA_ARGS__, __FILE__, __LINE__)

#define LOCATED0(fn) fn(__FILE__, __LINE__)

void print(char const* file_name, int line)
{
    printf("hello %s:%d\n", file_name, line);
}

int do_something(int value, char const* file_name, int line);

int main() {
    LOCATED0(print);
    LOCATED(do_something, 42);
}


在宏中执行工作,
#define print(){printf(“hello%d\n”,“LINE”uuuuuuu)}
。这正是我在上一个代码块中编写的内容,仅在
Do_stuff
上翻译,而不是
print
。我知道这个解决方案,并想知道是否有一个不那么繁琐的方法来做这件事(我有很多这样的函数。我需要为每个函数制作一个宏)。等等,不,对不起,我错了。您的方式使整个函数成为一个宏。这确实不同。我会考虑的,谢谢!MatthuBruChER不会像这样更改C++的标签。即使问题中的所有代码都用C编译器编译,也不会使问题成为C问题(注意默认参数是C++特性)。OP使用C++编译器,而不是C++。C中的工作是在宏、<代码>定义打印({你好(%D)n”、“γ-Lyn*”);我知道这个解决方案,并想知道是否有一个不那么繁琐的方法来做这件事(我有很多这样的函数。我需要为每个函数制作一个宏)。等等,不,对不起,我错了。您的方式使整个函数成为一个宏。这确实不同。我会考虑的,谢谢!MatthuBruChER不会像这样更改C++的标签。即使问题中的所有代码都用C编译器编译,也不会使问题成为C问题(注意默认参数是C++特性)。OP使用C++编译器,正在寻找C++答案,而不是C。这确实是我所要寻找的。但是我不能使用C++17(我想这需要什么?)。对不起,我忘了在问题中提到这一点。不过,我将来会记住这一点。谢谢@它不需要C++17,它要求标准库/编译器至少实现库基础知识TS v2的这一部分。您可以尝试一下,看看它是否有效,或者查看编译器/标准库的文档,看看它是否有效。这确实正是我想要的。但是我不能使用C++17(我想这需要什么?)。对不起,我忘了在问题中提到这一点。不过,我将来会记住这一点。谢谢@它不需要C++17,它要求标准库/编译器至少实现库基础知识TS v2的这一部分。您可以尝试一下,看看它是否有效,否则请查看编译器/标准库的文档,看看它是否有效。除了将其翻过来之外,我看不出这与
print(LOCATION)
(我目前在代码中是如何做的)有何不同。@pulp\u用户您是对的,它并不比
print(LOCATION)
,但这是另一种选择,除了把它翻过来,我看不出这与打印(位置)
(我目前在代码中是如何做的)有什么不同。@plup\u用户您是对的,它并不比打印(位置)
好,但它是一个alternative@Jarod42完成了。@Jarod42完成了。