- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一些类 C
并希望将其实例和方法的地址传递给测试函数 Test_C_Foo1()
中的某个仿函数。 Functor 是一个模板类,我必须提供类方法的类型 (MEMFN1
) 作为其模板参数之一。我必须在某处定义 MEMFN1
类型,但不想更改 C.h
并且不想用它污染全局命名空间。我决定尽可能将 typedef 本地化,所以把它放在一个测试函数中——在 MEMFN1
实际使用的范围内。 在函数体内使用 typedef 是一种好习惯吗?
标准允许在函数体内使用 typedef,仅在以下特定情况下限制它:
The typedef specifier shall not be combined in a decl-specifier-seq with any other kind of specifier except a type-specifier, and it shall not be used in the decl-specifier-seq of a parameter-declaration (8.3.5) nor in the decl-specifier-seq of a function-definition (8.4).
代码片段如下:
C.h:
...
#include <string>
...
class C
{
public:
int foo1(const std::string&);
};
main.cpp:
...
#include "C.h"
...
void Test_C_Foo1()
{
typedef int(C::*MEMFN1)(const std::string&);
C c;
Functor1<C, MEMFN1,...> f1(&c, &C1::foo1,...);
...
}
...
int main()
{
Test_C_Foo1();
return 0;
}
最佳答案
很好。它是合法的和本地化的。
关于c++ - 函数体内的 typedef 是一种不好的编程习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10103453/
我编写的代码主要供个人使用,但我正在考虑发布我最初开发供个人使用的应用程序(科学模拟/可视化)。 我的一个习惯是在类中使用一个main方法来单独测试类的运行情况。我认为这在某种程度上可能是不好的(毫无
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How do I convince programmers in my team to do TDD? 在从
假设我想测试是否有一个名为“Z”的驱动器。第一步是这样的; Get-PSProvider | Select-Object -Property Drives 这个给我; Drives: ... {C,
这是对 an old answer to a question about the necessity of functools.partial 的一种跟进: 虽然这个答案非常清楚地解释了这种现象及其
Perl 习惯很难改掉。两种语言之间的变量声明、作用域、全局/局部是不同的。是否有一组推荐的 python 语言习语可以使从 perl 编码到 python 编码的过渡不那么痛苦。 细微的变量拼写错误
我是一名优秀的程序员,十分优秀!