根据这个堆栈溢出答案,类型名称上的"_t"后缀在C中保留.当typedef用于创建新的opaque类型时,我习惯在名称中使用某种类型的指示.通常我会选择类似的东西,hashmap_t但现在我还需要别的东西.
C中的类型是否有任何标准命名方案?在其他语言中,使用类似CapsCase Hashmap是常见的,但我看到的很多C代码根本不使用大写.CapsCase也可以很好地使用库前缀,比如XYHashmap.
因此在C中命名类型是否有通用规则或标准?
在C++ 0X中使用基于范围的循环,我知道我们将能够:
std::vector<int> numbers = generateNumbers();
for( int k : numbers )
{
processNumber( k );
}
Run Code Online (Sandbox Code Playgroud)
(用lambda编写可能更简单)
但是,如果我只想将processNumber(k)应用于数字的一部分,我该怎么办?例如,我应该如何编写for for循环以将processNumber()应用于数字的一半(头部或尾部)?像Python或Ruby一样允许"切片"吗?
根据规范,不允许使用带有前导下划线的全局名称:
17.4.3.1.2全局名称
- 以下划线开头的每个名称都保留给实现,以用作全局名称空间中的名称.
这是否也适用于顶级匿名命名空间中定义的名称?
该<windows.h>头带有自己的BOOL类型.窥视实现,它似乎FALSE只是一个宏0,并且TRUE只是一个宏1,但我不确定这是否已指定.
将a转换BOOL为a 的惯用方法是bool什么?我可以想象很多可能的方法:
bool a = static_cast<bool>(x);
bool b = x ? true : false;
bool c = (x == TRUE);
bool d = (x != FALSE);
bool e = !!x;
// ...
Run Code Online (Sandbox Code Playgroud) 当我尝试运行此代码时:
Instructor.cpp:
#include "Instructor.h"
#include "Person.h"
using std::string;
Instructor::Instructor() {
Person();
salary = 0;
}
Instructor::Instructor(string n, string d, string g, int s) {
Person(n, d, g);
salary = s;
}
void Instructor::print() {
if (gender == "M")
std::cout << "Mr. ";
else
std::cout << "Ms. ";
Person::print();
std::cout << " Instructor, Salary: " << salary << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
Instructor.h:
#include <iostream>
#include <string>
class Instructor: public Person
{
public:
Instructor();
Instructor(std::string n, std::string d, std::string g, int s);
virtual …Run Code Online (Sandbox Code Playgroud) 我们假设以下代码
int __foo(void) {
return 0;
}
int _BAR(void) {
return 3;
}
int main(void) {
return __foo() & _BAR();
}
Run Code Online (Sandbox Code Playgroud)
双下划线和单个下划线后跟一个大写字母符号是保留的,因此不允许(这是一个C++问题,但它也提到了C规则).
我尝试了-Wall -Wextra -pedantic关于gcc的-Weverything选项和关于clang的选项,两者都没有对此发出警告.
有没有办法为此启用编译器警告?
这个问题很可能是"如何将字符串映射到枚举"的第n次迭代.
我的要求更进一步,throw当在有效输入范围内找不到密钥时,我想要一个例外.所以我有这个实现EnumMap(需要提升const std::map定义):
#include <map>
#include <string>
#include <sstream>
#include <stdexcept>
#include <boost/assign.hpp>
typedef enum colors {
RED,
GREEN,
} colors;
// boost::assign::map_list_of
const std::map<std::string,int> colorsMap = boost::assign::map_list_of
("red", RED)
("green", GREEN);
//-----------------------------------------------------------------------------
// wrapper for a map std::string --> enum
class EnumMap {
private:
std::map<std::string,int> m_map;
// print the map to a string
std::string toString() const {
std::string ostr;
for(auto x : m_map) {
ostr += x.first + ", ";
} …Run Code Online (Sandbox Code Playgroud) 长话短说,gcc和vc ++预处理器具有相同输入的不同输出.如果传递给另一个宏,vc ++中的变量宏似乎不会进行"参数匹配"(如果它是正确的术语).例如:
#define MACRO(a, ...) head:a, tail:MACRO_OTHER(__VA_ARGS__)
#define MACRO_OTHER(a, ...) head:a, tail:__VA_ARGS__
Run Code Online (Sandbox Code Playgroud)
同
MACRO(1, 2, 3, 4, 5)
Run Code Online (Sandbox Code Playgroud)
gcc输出:
head:1, tail:head:2, tail:3,4,5
Run Code Online (Sandbox Code Playgroud)
vc ++输出:
head:1, tail:head:2,3,4,5, tail:
Run Code Online (Sandbox Code Playgroud)
显然,a在MACRO_OTHER是2,3,4,5与空可变参数部分.考虑到这一点,有没有办法创建一个vc ++替代以下的宏(这与gcc很好)
#define VA_TYPES_WITH_ARGS(...) __VA_TYPES_WITH_ARGS(VA_NUM_ARGS(__VA_ARGS__),##__VA_ARGS__)
#define __VA_TYPES_WITH_ARGS(n, ...) _VA_TYPES_WITH_ARGS(n,##__VA_ARGS__)
#define _VA_TYPES_WITH_ARGS(n, ...) _VA_TYPES_WITH_ARGS_##n(__VA_ARGS__)
#define _VA_TYPES_WITH_ARGS_0()
#define _VA_TYPES_WITH_ARGS_1(type ) type _arg1
#define _VA_TYPES_WITH_ARGS_2(type, ...) type _arg2, _VA_TYPES_WITH_ARGS_1(__VA_ARGS__)
#define _VA_TYPES_WITH_ARGS_3(type, ...) type _arg3, _VA_TYPES_WITH_ARGS_2(__VA_ARGS__)
// etc
Run Code Online (Sandbox Code Playgroud)
它基本上附加_argK每个参数.
例:
VA_TYPES_WITH_ARGS(int, bool, float)
Run Code Online (Sandbox Code Playgroud)
将扩大到
int _arg3, bool _arg2, …Run Code Online (Sandbox Code Playgroud) 我有以下类定义:
class image {
public:
template <class T> image(int w, int h);
virtual ~image();
void clear();
};
template <class T> image::image(int w, int h) {
...
...
}
Run Code Online (Sandbox Code Playgroud)
构造函数在同一个头文件中定义.现在,我如何实例化这个类的对象?
我试过这个
image a<float>(128, 128);
Run Code Online (Sandbox Code Playgroud)
但得到以下错误
error: expected initializer before '<' token
image a<float>(128, 128);
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?