我一直在研究解析器commands(它是围绕大型数据数据的奇特包装器),并且有一个未处理命令所在的队列.如果我需要一个命令,我用这样的代码查询它:
boost::optional<command> get_command() {
if (!has_command()) return boost::optional<command>(nullptr);
else {
boost::optional<command> comm(command_feed.front()); //command_feed is declared as a std::queue<command>
command_feed.pop();
return comm;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,在适当的情况下,这些命令的大小可能是兆字节,并且需要非常快速地解析.我的想法是,我可以优化转移到这样的移动:
boost::optional<command> get_command() {
if (!has_command()) return boost::optional<command>(nullptr);
else {
boost::optional<command> comm(std::move(command_feed.front())); //command_feed is declared as a std::queue<command>
command_feed.pop();
return comm;
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎适用于这种特定情况,但它可以用作任何正确维护的RAII对象的通用解决方案,还是我应该做其他事情?
我有一些计时代码,我用它来衡量给定代码片段的运行时间:
struct time_data {
std::chrono::steady_clock::time_point start, end;
auto get_duration() const {
return end - start;
}
void print_data(std::ostream & out) const {
out << str();
}
std::string str() const {
static const std::locale loc{ "" };
std::stringstream ss;
ss.imbue(loc);
ss << "Start: " << std::setw(24) << std::chrono::nanoseconds{ start.time_since_epoch() }.count() << "ns\n";
ss << "End: " << std::setw(24) << std::chrono::nanoseconds{ end.time_since_epoch() }.count() << "ns\n";
ss << "Duration: " << std::setw(24) << std::chrono::nanoseconds{ get_duration() }.count() << "ns\n";
return ss.str();
}
static friend …Run Code Online (Sandbox Code Playgroud) 对于上下文,我正在使用的实际类比我在这里展示的更复杂和更大,但我只是作为一个例子使用它.
struct Vector {
int x, y;
Vector() : Vector(0,0) {}
Vector(int x, int y) : x(x), y(y) {}
};
Run Code Online (Sandbox Code Playgroud)
我想添加运算符重载以允许Vectors相互相加和相减.
Vector& operator+=(Vector const& v) {
x += v.x;
y += v.y;
return *this;
}
Vector operator+(Vector const& v) const {
return Vector(*this) += v;
}
Vector& operator-=(Vector const& v) {
x -= v.x;
y -= v.y;
return *this;
}
Vector operator-(Vector const& v) const {
return Vector(*this) -= v;
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码可以允许不幸的结构:
int main() {
Vector & a …Run Code Online (Sandbox Code Playgroud) 给定两个或更多示例函数,是否可以编写模板化代码,这些代码能够推导出作为模板参数提供的函数的参数?
这是一个激励的例子:
void do_something(int value, double amount) {
std::cout << (value * amount) << std::endl;
}
void do_something_else(std::string const& first, double & second, int third) {
for(char c : first)
if(third / c == 0)
second += 13.7;
}
template<void(*Func)(/*???*/)>
struct wrapper {
using Args = /*???*/;
void operator()(Args&& ... args) const {
Func(std::forward<Args>(args)...);
}
};
int main() {
wrapper<do_something> obj; //Should be able to deduce Args to be [int, double]
obj(5, 17.4); //Would call do_something(5, 17.4);
wrapper<do_something_else> obj2; //Should …Run Code Online (Sandbox Code Playgroud) 我决定将我的boost库从1.61更新到1.63,在我更新的项目中使用新文件,我收到一些我以前没有得到的新错误消息:
error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ)
error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ)
Run Code Online (Sandbox Code Playgroud)
由于我的1.63库是使用Visual Studio 2017编译的,我的第一个假设是我在编译boost库时犯了一个错误,所以这里是我从boost文件的一个干净解压缩中得到的总步骤:
boost_1_63_0文件夹中.bootstrap.batproject-config.jam进行编辑using msvc ;改为using msvc : 14.1 : E:\Program Files\Microsoft Visual Studio\VC\Tools\MSVC\14.10.24911\bin\HostX64\x64\;boost/config/auto_link.hpp进行编辑b2 architecture=x86 address-model=64 link=static threading=multi runtime-link=shared --build-type=complete stage --stagedir=stage/x64 -a#define BOOST_LIB_DIAGNOSTIC用于跟踪正在使用的文件(它们是).有谁知道我的错误在哪里?如果我使用在Visual Studio 2017 RC中使用Visual Studio …
当我用外国人写字时(法语......)似乎有问题
例如,如果我要求输入std :: string或char [],如下所示:
std::string s;
std::cin>>s; //if we input the string "café"
std::cout<<s<<std::endl; //outputs "café"
Run Code Online (Sandbox Code Playgroud)
一切都好.
虽然字符串是硬编码的
std::string s="café";
std::cout<<s<<std::endl; //outputs "cafÚ"
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?C++支持哪些字符,如何使其正常工作?它与我的操作系统(Windows 10)有关吗?我的IDE(VS 15)?还是用C++?
我一直在尝试使用boost.multiprecision在我的VC2017项目中工作时遇到问题,并且我尝试将最简单的项目作为概念证明:
#include<boost/multiprecision/cpp_int.hpp>
int main() {
boost::multiprecision::cpp_int val{ 5 };
val *= 5;
val *= 5;
return val.convert_to<int>();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,此代码无法编译,并出现以下错误:
1>------ Build started: Project: Multiprecision Test, Configuration: Debug x64 ------
1>Multi Main.cpp
1>Unknown compiler version - please run the configure tests and report the results
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(36): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(40): note: see reference to class template instantiation 'boost::equal_pointees_t<OptionalPointee>' being compiled
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(59): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(63): note: see reference to class template …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写符合RAII的资源包装器,而我却陷入了如何形成模板参数的语义.
例如,我可以编写一个函数来删除我的资源:
void int_cleaner(int val) {
std::cout << "Value of " << val << " has been cleaned up." << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
或者我可以把它写成一个Functor:
struct int_deleter {
void operator()(int val) const {
std::cout << "Value of " << val << " has been cleaned up." << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
但是我遇到了困难:如果我想将它传递给我的资源包装器,我必须更改模板参数的定义方式.
如果我这样写resource:
template<typename T, typename Deleter>
class resource {
};
Run Code Online (Sandbox Code Playgroud)
这适用于仿函数,但不适用于函数本身.
int main() {
resource<int, int_deleter> res; //Compiles fine
//resource<int, int_cleaner> res2; //Does Not Compile
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相反,如果我写这样的模板参数:
template<typename …Run Code Online (Sandbox Code Playgroud) 如果打开任意项目的属性,则可以更改任何目标项目的工作目录:
但是,如果我尝试为项目打开单个属性表,则“调试”类别丢失,并且找不到任何可以更改的字段,这将允许我为该属性表设置工作目录(因此,请允许我同时更改多个项目的工作目录):
如何在属性表级别更改此属性?
当我写持有资源类,我很习惯写一个简单的交换功能,以简化传输/复印资源的过程.以下代码是一个简单的例子:
class MyArray {
public:
MyArray(size_t size) :
_array(size ? new int[size] : nullptr),
_size(size)
{
}
MyArray(const MyArray & mv) :
MyArray(mv._size)
{
if(mv._size) std::copy(mv._array, mv._array + mv._size, _array);
}
static void friend swap(MyArray & mv1, MyArray & mv2) noexcept {
std::swap(mv1._array, mv2._array);
std::swap(mv1._size, mv2._size);
}
MyArray(MyArray && mv) {
swap(*this, mv);
}
MyArray & operator=(MyArray mv) {
swap(*this, mv);
return *this;
}
~MyArray() noexcept {
delete[] _array;
}
int & operator[](size_t index) {
if(index >= _size) throw std::exception("Index …Run Code Online (Sandbox Code Playgroud)