gpt4 book ai didi

c++ - 在 C++ 中声明内联但实际上内联函数是否合法

转载 作者:行者123 更新时间:2023-11-30 00:45:36 25 4
gpt4 key购买 nike

在头文件中:

inline void func(void); // declare only, with `inline`

在impl源文件中:

void func(void) { balabala(); }

在其他源文件中:

func(); // call the func

问题:声明内联函数是否合法,即使它实际上并未内联在头文件中?

PS:

Why need this: I have some macro generated functions, may or may not be declare in header only, so I wish the macro can be used without explicitly specify inline or not

And, I know the function can be wrapped by a wrapper class as static member function in header

the tricky inline solution was tested under MSVC and clang without compile error, simply want to know whether it's legal in C++ standard

最佳答案

这是不合法的。来自 cppreference.com :

2) The definition of an inline function or variable (since C++17) must be present in the translation unit where it is accessed (not necessarily before the point of access).

关于c++ - 在 C++ 中声明内联但实际上内联函数是否合法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42932453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com