我试图在C++ 11代码中使用std :: regex,但看起来支持有点儿错误.一个例子:
#include <regex>
#include <iostream>
int main (int argc, const char * argv[]) {
std::regex r("st|mt|tr");
std::cerr << "st|mt|tr" << " matches st? " << std::regex_match("st", r) << std::endl;
std::cerr << "st|mt|tr" << " matches mt? " << std::regex_match("mt", r) << std::endl;
std::cerr << "st|mt|tr" << " matches tr? " << std::regex_match("tr", r) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
st|mt|tr matches st? 1
st|mt|tr matches mt? 1
st|mt|tr matches tr? 0
Run Code Online (Sandbox Code Playgroud)
当使用gcc(MacPorts gcc47 4.7.1_2)4.7.1编译时,使用
g++ *.cc -o test -std=c++11 …
Run Code Online (Sandbox Code Playgroud) 有什么区别
int x=7;
Run Code Online (Sandbox Code Playgroud)
和
register int x=7;
Run Code Online (Sandbox Code Playgroud)
?
我正在使用C++.
我对使用有点困惑std::condition_variable
.我明白我必须创建unique_lock
一个mutex
调用之前condition_variable.wait()
.我无法找到是我是否也应该调用之前获得一个独特的锁notify_one()
或notify_all()
.
cppreference.com上的示例存在冲突.例如,notify_one页面提供了以下示例:
#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>
std::condition_variable cv;
std::mutex cv_m;
int i = 0;
bool done = false;
void waits()
{
std::unique_lock<std::mutex> lk(cv_m);
std::cout << "Waiting... \n";
cv.wait(lk, []{return i == 1;});
std::cout << "...finished waiting. i == 1\n";
done = true;
}
void signals()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Notifying...\n";
cv.notify_one();
std::unique_lock<std::mutex> lk(cv_m);
i = 1;
while (!done) {
lk.unlock();
std::this_thread::sleep_for(std::chrono::seconds(1)); …
Run Code Online (Sandbox Code Playgroud) 我阅读了维基百科关于C++中用于执行静态(读取:编译时)多态的奇怪重复模板模式的文章.我想概括它,以便我可以根据派生类型更改函数的返回类型.(这似乎应该是可能的,因为基类型知道模板参数中的派生类型).不幸的是,以下代码将无法使用MSVC 2010进行编译(我现在没有轻松访问gcc所以我还没有尝试过).谁知道为什么?
template <typename derived_t>
class base {
public:
typedef typename derived_t::value_type value_type;
value_type foo() {
return static_cast<derived_t*>(this)->foo();
}
};
template <typename T>
class derived : public base<derived<T> > {
public:
typedef T value_type;
value_type foo() {
return T(); //return some T object (assumes T is default constructable)
}
};
int main() {
derived<int> a;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我有一个使用额外模板参数的解决方法,但我不喜欢它 - 当在继承链上传递许多类型时会变得非常冗长.
template <typename derived_t, typename value_type>
class base { ... };
template <typename T>
class derived : public …
Run Code Online (Sandbox Code Playgroud) 我还远未完全理解C++链接器的工作原理,我对此有一个特定的问题.
说我有以下内容:
Utils.h
namespace Utils
{
void func1();
void func2();
}
Run Code Online (Sandbox Code Playgroud)
Utils.cpp
#include "some_huge_lib" // needed only by func2()
namespace Utils
{
void func1() { /* do something */ }
void func2() { /* make use of some functions defined in some_huge_lib */ }
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
int main()
{
Utils::func1()
}
Run Code Online (Sandbox Code Playgroud)
我的目标是生成尽可能小的二进制文件.
我的问题是,是否some_huge_lib
会包含在输出对象文件中?
标准C++库中的所有名称都是小写的,除了std::ios_base::Init
.为什么是这样?
C++ 11标准对标准库相关的自移动赋值有何看法?更具体的是,什么(如果有的话)保证什么selfAssign
呢?
template<class T>
std::vector<T> selfAssign(std::vector<T> v) {
v = std::move(v);
return v;
}
Run Code Online (Sandbox Code Playgroud) 很明显constexpr意味着const,因此常见的是:
constexpr int foo = 42; // no const here
Run Code Online (Sandbox Code Playgroud)
但是,如果你写:
constexpr char *const str = "foo";
Run Code Online (Sandbox Code Playgroud)
然后,如果传递了-Wwrite-string标志,GCC将产生"警告:从字符串常量弃用转换为'char*'".
写作:
constexpr const char *const str = "foo";
Run Code Online (Sandbox Code Playgroud)
解决了这个问题.
那么constexpr const和constexpr真的一样吗?
我想我从C++标准库遇到了std :: poisson_distribution的错误行为.
以下C++代码(文件poisson_test.cc)用于生成泊松分布数:
#include <array>
#include <cmath>
#include <iostream>
#include <random>
int main() {
// The problem turned out to be independent on the engine
std::mt19937_64 engine;
// Set fixed seed for easy reproducibility
// The problem turned out to be independent on seed
engine.seed(1);
std::poisson_distribution<int> distribution(157.17);
for (int i = 0; i < 1E8; i++) {
const int number = distribution(engine);
std::cout << number << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我按如下方式编译此代码:
clang++ -o poisson_test -std=c++11 poisson_test.cc …
Run Code Online (Sandbox Code Playgroud) C++标准库头可以以未指定的方式彼此包含,因此程序员通常不应该依赖于包括另一个的一个头.但是,在少数情况下,保证标头包含另一个标头,或者使某些功能可用,否则需要包含另一个标头.这些案件是什么?