C/C ++ Rarer关键字-寄存器,易失性,外部,显式 [英] C/C++ Rarer keywords - register, volatile, extern, explicit

查看:40
本文介绍了C/C ++ Rarer关键字-寄存器,易失性,外部,显式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否快速介绍一下这4个关键字的用途以及原因?

Can you give me a quick runthrough of what these 4 keywords are used for and why?

我了解google在注册时会告诉您的基本知识和易变的知识,但我想了解更多(只是实用的概述).外部和显式使我有些困惑,因为尽管编写了相当低级的嵌入式系统代码,但我从来没有找到自己必须使用它们的理由.再说一次,我可以谷歌搜索,但我希望有专家提供的快速,实用的摘要,使它牢记在心.

I understand the basics that google would tell you on register and volatile, but would like to know a little more (just a practical overview). Extern and explicit confuse me a little as I've never found a reason to have to use them myself despite doing fairly low-level embedded systems code. Again, I can google but I'd prefer a quick, practical summary from an expert so it sticks in my mind.

推荐答案

外部

extern 已过载,有多种用途.对于全局变量,这意味着它正在声明变量,而不是对其进行定义.这对于将全局变量放在标头中很有用.如果将其放在标题中:

extern

extern is overloaded for several uses. For global variables, it means that it is declaring the variable, not defining it. This is useful for putting global variables in headers. If you put this in a header:

int someInteger;

每个包含该标头的.cpp文件都将尝试具有自己的 someInteger .这将导致链接器错误.通过 extern 进行声明,您要说的是在代码中的某个地方会有 someInteger :

Each .cpp file that includes that header would try to have its own someInteger. That will cause a linker error. By declaring it with extern, all you're saying is that there will be a someInteger somewhere in the code:

extern int someInteger;

现在,在.cpp文件中,您可以定义 int someInteger ,这样就只有一个副本.

Now, in a .cpp file, you can define int someInteger, so that there will be exactly one copy of it.

还有 extern"C" ,用于指定某些函数使用C-linkage规则而不是C ++.这对于与库和编译为C的代码进行接口很有用.

There is also extern "C", which is used for specifying that certain functions use C-linkage rules rather than C++. This is useful for interfacing with libraries and code compiled as C.

在C ++ 0x中,还将有 extern template 声明.这些与显式模板实例化相反.当您这样做时:

In C++0x, there will also be extern template declarations. These are the opposite of explicit template instantiation. When you do this:

template class std::vector<int>;

您要告诉编译器立即实例化此模板.通常,实例化将延迟到首次使用模板之前.在C ++ 0x中,您可以说:

You're telling the compiler to instantiate this template right now. Normally, the instantiation is delayed until the first use of the template. In C++0x, you can say:

extern template class std::vector<int>;

这告诉编译器 not ever 这个.cpp文件中实例化此模板.这样,您可以控制模板的实例化位置.明智地使用它可以大大缩短编译时间.

This tells the compiler not to instantiate this template in this .cpp file, ever. That way, you can control where templates are instantiated. Judicious use of this can substantially improve compile times.

这用于防止类型的自动转换.如果您的类 ClassName 具有以下构造函数:

This is used to prevent automatic conversions of types. If you have a class ClassName with the following constructor:

ClassName(int someInteger);

这意味着,如果您有一个采用 ClassName 的函数,则用户可以使用 int 进行调用,并且转换将自动完成.

This means that if you have a function that takes a ClassName, the user can call it with an int, and the conversion will be done automatically.

void SomeFunc(const ClassName &className);
SomeFunc(3);

这是合法的,因为 ClassName 具有一个采用整数的转换构造函数.这就是采用 std :: string 的函数也可以采用 char * 的方式. std :: string 具有一个采用 char * 的构造函数.

That's legal, because ClassName has a conversion constructor that takes an integer. This is how functions that take std::string can also take a char*; std::string has a constructor that takes a char*.

但是,大多数情况下,您不希望这样的隐式转换.通常,您只希望转换是明确的.是的,它有时和std :: string一样有用,但是您需要一种关闭不适当转换的方法.输入 explicit :

However, most of the time you don't want implicit conversions like this. You only usually want conversions to be explicit. Yes, it's sometimes useful as with std::string, but you need a way to turn it off for conversions that are inappropriate. Enter explicit:

explicit ClassName(int someInteger);

这将防止隐式转换.您仍然可以使用 SomeFunc(ClassName(3)); ,但是 SomeFunc(3)将不再起作用.

This will prevent implicit conversions. You can still use SomeFunc(ClassName(3)); but SomeFunc(3) will no longer work.

顺便说一句:如果 explicit 对您来说很少,那么您使用它的机会就不多了.除非特别需要转换,否则您应始终使用它.

BTW: if explicit is rare for you, then you're not using it nearly enough. You should use it at all times, unless you specifically want conversion. Which is not that often.

这会阻止某些有用的优化.通常,如果您有一个变量,则C/C ++会假定仅在显式更改内容的情况下它的内容才会更改.因此,如果将 int someInteger; 声明为全局变量,则C/C ++编译器可以在本地缓存该值,而不必在每次使用它时都不断访问该值.

This prevents certain useful optimizations. Normally, if you have a variable, C/C++ will assume that it's contents will only change if it explicitly changes them. So if you declare a int someInteger; as a global variable, C/C++ compilers can cache the value locally and not constantly access the value every time you use it.

有时,您想停止此操作.在这种情况下,您可以使用 volatile ;这会阻止这些优化.

Sometimes, you want to stop this. In those cases, you use volatile; this prevents those optimizations.

这只是一个提示.它告诉编译器尝试将变量的数据放入寄存器中.本质上是不必要的.编译器在决定什么应该和不应该成为寄存器方面比您要好.

This is just a hint. It tells the compiler to try to put the variable's data in a register. It's essentially unnecessary; compilers are better than you are at deciding what should and should not be a register.

这篇关于C/C ++ Rarer关键字-寄存器,易失性,外部,显式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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