在命名空间中使变量和函数内联和静态是否有任何意义。例如,考虑以下2个文件:
考虑第一个头文件
// header1.h
#include <string>
#include <iostream>
namespace First
{
static inline std::string s {"hi"};
static inline void print(std::string str)
{
std::cout << str << std::end;
}
}并考虑下面的第二个头文件
// header2.h
#include <string>
#include <iostream>
namespace Second
{
std::string s {"hi"};
void print(std::string str)
{
std::cout << str << std::end;
}
}编译器对两种代码的看法是不同的还是它们引入了相似的行为?
发布于 2022-06-21 05:41:46
没有命名空间变量和函数有内部链接,比如未命名的命名空间?
不,您认为名称空间(命名名称空间)变量和函数默认具有内部链接的假设是不正确的。
编译器对两种代码的看法是不同的还是它们引入了相似的行为?
带有static关键字的一个具有内部链接,而另一个(在标题2.h中)具有外部链接。
也就是说,头1.h和each 2.h中的变量s是不同的。同样,这些功能也是不同的。
header1.h 1.h
namespace First
{
static inline std::string s {"hi"}; //s has internal linkage
static inline void print(std::string str) //print has internal linkage
{
std::cout << str << std::end;
}
}header2.h
namespace Second
{
std::string s {"hi"}; //s has external linkage
void print(std::string str) //print has external linkage
{
std::cout << str << std::end;
}
}https://stackoverflow.com/questions/72695765
复制相似问题