对“内联”的理解包括:在C ++类构造函数中? [英] Understanding of "inline" in C++ class constructor?

查看:80
本文介绍了对“内联”的理解包括:在C ++类构造函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在boost库中读取了类 address_v4 的源代码,并且有几个用BOOST_ASIO_DECL声明的构造函数(定义为内联)

I read a source code of class address_v4 in boost library and there are several constructors declared with BOOST_ASIO_DECL (defined as inline)

/// Construct an address from raw bytes.
BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);

/// Construct an address from a unsigned long in host byte order.
BOOST_ASIO_DECL explicit address_v4(unsigned long addr);

(从此处 http://www.boost.org/doc/libs/1_64_0/boost/asio/ip/address_v4.hpp ))

# define BOOST_ASIO_DECL inline

(从此处 http: //www.boost.org/doc/libs/1_64_0/boost/asio/detail/config.hpp

那么,指定目的是什么C ++构造函数的内联?它与函数的含义相同还是具有不同的含义?

So, what the purpose of specifing "inline" for c++ constructor ? Is it the same meaning as for functions or it has a different meaning ?

推荐答案

它的含义完全相同(与一些程序员花花公子指出ctor是一个与其他函数一样的函数),但是鉴于任何编译器(我能想到的)都会很乐意忽略您的建议(以决定是否需要内联)。必须有其他用途...

It has exactly the same meaning (as some programmer dude noted the ctor is a function like any other) but given that any compiler (I can think of) will happily ignore your suggestion (to decide if inline is required or not using all his knowledge) then it has to have another use...

实际上,它可以在头文件中定义一个函数(或ctor),而不会由于一个定义规则

In fact it's there to define a function (or ctor) in your header file without the errors linker will raise because of the one definition rule.

请注意,当函数没有主体时,它将打开另一个场景:以多个翻译单位定义函数。在这种情况下,每个实现都必须相同(AFAIK,如果我输入错了,请更正我)或UB。

Note that it opens another scenario when function has not a body: to define the function in multiple translation units. In this case each implementation must be the same (AFAIK, please correct me if I'm wrong) or it's UB.

简而言之:在头文件中定义一个函数,并在多个翻译单元中使用它:链接器错误。将其标记为内联并且错误消失了(这并不意味着该函数已真正内联)。

In short: define a function in your header file and use it in multiple translation units: linker error. Mark it as inline and errors are gone (and it won't imply that function is really inlined).

这篇关于对“内联”的理解包括:在C ++类构造函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆