ISO C++03 对函数作用域定义的结构体有哪些限制?
我们不允许在函数内定义函子结构,因为不允许在函数模板的实例化中使用函数声明的结构。
还有其他需要注意的重大陷阱吗?例如,这会很糟糕吗:
int foo()
{
struct Scratch
{
int a, b, c;
};
std::vector<Scratch> workingBuffer;
//Blah Blah
}
We're not allowed to define a functor struct inside a function because one is not allowed to use function declared structs in the instantiation of function templates.
Are there any other significant pitfalls to be aware of? E.g. would this be bad:
int foo()
{
struct Scratch
{
int a, b, c;
};
std::vector<Scratch> workingBuffer;
//Blah Blah
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
1。 C++ 标准禁止使用带有模板的本地定义类。
14.3.1/2:本地类型、没有链接的类型、未命名类型或由任何这些类型组合而成的类型不得用作模板类型参数的模板参数。
代码示例:
以下是来自 IBM 文档的更多参考:
2.本地类中的声明只能使用封闭范围中的类型名称、枚举、静态变量以及外部变量和函数。
代码示例:
3.本地类不能有静态数据成员
代码示例:
1. C++ standard forbids using locally-defined classes with templates.
14.3.1/2: A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.
A code example:
Here is a little more reference from IBM documentation:
2. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.
A Code Example:
3. A local class cannot have static data members
A Code Example:
本地类的范围是定义它们的函数。但这本身并不有趣1。
本地类的有趣之处在于如果它们实现了某个接口,那么您可以创建它的实例(使用
new
)并返回它们,从而使实现可以通过基类指针甚至在函数外部访问。有关本地类的其他一些事实:
它们不能定义静态成员变量。
它们无法访问封闭函数的非静态“自动”局部变量。但它们可以访问
静态
变量。它们可以在模板函数中使用。但是,它们不能用作模板参数。
如果他们在模板函数内部定义,那么他们可以使用外围函数的模板参数。
本地类是最终的,这意味着函数外部的用户不能从本地类派生到函数。如果没有本地类,您必须在单独的翻译单元中添加未命名的命名空间。
本地类用于创建trampoline 函数,通常称为 thunk。
标准 (2003)
9.8 局部类声明 [class.local]
The scope of the local classes is the function in which they're defined.But that isn't interesting in itself1.
What makes local classes interesting is that if they implement some interface, then you can create instances of it (using
new
) and return them, thereby making the implementation accessible through the base class pointer even outside the function.Some other facts about local classes:
They cannot define static member variables.
They cannot access nonstatic "automatic" local variables of the enclosing function. But they can access the
static
variables.They can be used in template functions. They cannot be used as template argument, however.
If they defined inside template function, then they can use the template parameters of the enclosing function.
Local classes are final, that means users outside the function cannot derive from local class to function. Without local classes, you'd have to add an unnamed namespace in separate translation unit.
Local classes are used to create trampoline functions usually known as thunks.
Some references from the Standard (2003)
9.8 Local class declarations [class.local]
本地结构/类不能有静态数据成员,只能有静态成员函数。此外,它们不能成为模板。
Local structs / classes can't have static data members, only static member functions. Also, they can't be templates.
是的。 C++03 中局部类不能用作模板参数
Yes. Local classes can't be used as template parameters in C++03
本地结构是完全合法的,即使在 C++98 中也是如此。不过,您不能在 C++98 中将它们与模板一起使用,而在 C++0x 中则可以。 g++ 4.5 支持在 -std=c++0x 模式下使用带有模板的本地结构。
local structs are perfectly legal, even in C++98. You cannot use them with templates in C++98 though, whereas you can in C++0x. g++ 4.5 supports using local structs with templates in -std=c++0x mode.