有没有办法用C++ 11功能替换Xmacro习语,最好不要使用预处理器?我认为可以使用元组模板,但我仍然试图弄清楚这些是如何工作的.
所以我一直在使用GCC 4.6进入新的C++,它现在有基于范围的for循环.我发现这对迭代数组和向量非常好.
出于主要的美学原因,我想知道是否有办法用它来取代标准
for(int i = min; i < max; i++) {}
喜欢的东西
for(int& i : std::range(min, max)) {}
新的C++标准中是否有内置的东西允许我这样做?或者我必须编写自己的范围/迭代器类?
我想我在这里错过了一些非常愚蠢的东西.
我安装了libcppunit :(我使用的是Ubuntu 12.04)
$ apt-cache policy libcppunit-dev
libcppunit-dev:
Installed: 1.12.1-4
Candidate: 1.12.1-4
Version table:
*** 1.12.1-4 0
500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
100 /var/lib/dpkg/status
$ apt-cache policy libcppunit-1.12-1
libcppunit-1.12-1:
Installed: 1.12.1-4
Candidate: 1.12.1-4
Version table:
*** 1.12.1-4 0
500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
100 /var/lib/dpkg/status
Run Code Online (Sandbox Code Playgroud)
我有一个简单的测试:
#include <iostream>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
int main() {
CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::TextUi::TestRunner runner;
runner.addTest(suite);
runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
return runner.run() ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)
我这是编译器输出:
$ g++ …Run Code Online (Sandbox Code Playgroud) 我今天开始学习Dart,而且我遇到了一些谷歌技能难以找到的东西.
如何在非空案例中进行翻版?
我的用例是这样的:我正在写一个sprintf实现(因为dart也没有这个),除了这个落空的东西之外它会工作.例如,在解析变量类型时,您可以使用"%x"与"%X",其中大写类型告诉格式化程序输出应该是大写的.
半伪代码看起来像:
bool is_upper = false;
switch (getType()) {
case 'X':
is_upper = true;
case 'x':
return formatHex(is_upper);
}
Run Code Online (Sandbox Code Playgroud)
我可以想到的其他方式,将是以下之一
1:
switch (getType()) {
case 'X': case 'x':
return formatHex('X' == getType());
}
Run Code Online (Sandbox Code Playgroud)
2:
var type = getType();
if (type in ['x', 'X']) {
return formatHex('X' == getType());
}
Run Code Online (Sandbox Code Playgroud)
现在,第二个选择几乎看起来不错,但是你必须记住,有11个案例,这意味着有11个if (type in []),这是我想要的更多打字.
那么,飞镖有一些// //$FALL-THROUGH$我不知道的吗?
谢谢.
我正在将我的一个C++项目(一个简单的DSL)转换为生锈学习生锈,我遇到嵌套结构和所有权问题.我很难转换一些像:
struct FileData {
bool is_utf8;
std::string file_name;
};
class Token {
public:
enum TokenType {
REGULAR,
INCLUDE_FILE,
}
Token() {
_type = REGULAR;
}
Type get_type() const { return _type; }
void beginIncludeFile() {
_type = INCLUDE_FILE;
_include_data = std::unique_ptr<FileData>(new FileData);
}
bool is_utf8() const {
assert(get_type() == INCLUDE_FILE);
return _include_data->is_utf8;
}
void set_utf8(bool value) {
assert(get_type() == INCLUDE_FILE);
_include_data->is_utf8 = value;
}
const std::string& get_file_name() const {
assert(get_type() == INCLUDE_FILE);
return _include_data->file_name;
}
void setFileNameToEmpty() {
assert(get_type() == …Run Code Online (Sandbox Code Playgroud)