小编Xir*_*ema的帖子

是否可以从队列中"移动"一个对象,如果你要从中弹出它?

我一直在研究解析器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对象的通用解决方案,还是我应该做其他事情?

c++ boost stl optional c++11

16
推荐指数
2
解决办法
6826
查看次数

当我使用比我的CPU有核心更多的线程时,为什么这段代码变得更快?

我有一些计时代码,我用它来衡量给定代码片段的运行时间:

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)

c++ performance multithreading

15
推荐指数
1
解决办法
429
查看次数

如何为运营商正确编写R-Value重载

对于上下文,我正在使用的实际类比我在这里展示的更复杂和更大,但我只是作为一个例子使用它.

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)

c++ undefined-behavior move-semantics c++11

10
推荐指数
1
解决办法
266
查看次数

如何从函数指针中删除参数列表?

给定两个或更多示例函数,是否可以编写模板化代码,这些代码能够推导出作为模板参数提供的函数的参数?

这是一个激励的例子:

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)

c++ templates template-meta-programming

8
推荐指数
2
解决办法
858
查看次数

LNK2001:我的boost库(可能)构建不正确

我决定将我的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文件的一个干净解压缩中得到的总步骤:

  1. 开始菜单→Visual Studio 2017 RC→开发人员命令提示符
  2. 我更改目录,直到我在高级boost_1_63_0文件夹中.
  3. 我跑 bootstrap.bat
  4. 我打开project-config.jam进行编辑
  5. using msvc ;改为using msvc : 14.1 : E:\Program Files\Microsoft Visual Studio\VC\Tools\MSVC\14.10.24911\bin\HostX64\x64\;
  6. 我打开boost/config/auto_link.hpp进行编辑
  7. 我对此文件进行了编辑(列表后面列出的代码)
  8. 在打开命令提示符下,我执行命令 b2 architecture=x86 address-model=64 link=static threading=multi runtime-link=shared --build-type=complete stage --stagedir=stage/x64 -a
  9. 它在最后以下面的消息完成(在列表后面列出)
  10. 我尝试将这些库与我的代码一起使用,#define BOOST_LIB_DIAGNOSTIC用于跟踪正在使用的文件(它们是).
  11. 我尝试编译使用boost.asio的项目,并获得上面列出的两个未解决的外部符号错误.

有谁知道我的错误在哪里?如果我使用在Visual Studio 2017 RC中使用Visual Studio …

c++ boost visual-studio lnk2001

7
推荐指数
1
解决办法
1539
查看次数

C++支持的字符

当我用外国人写字时(法语......)似乎有问题

例如,如果我要求输入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++?

c++ unicode character-encoding c++14

6
推荐指数
1
解决办法
807
查看次数

C2143/C2518尝试使用boost.multiprecision编译项目时

我一直在尝试使用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)

c++ boost boost-multiprecision visual-studio-2017

6
推荐指数
1
解决办法
1084
查看次数

模板可以接受函数指针或Functor的参数

我正在尝试编写符合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)

c++ templates template-meta-programming

6
推荐指数
1
解决办法
561
查看次数

Visual Studio:如何在属性表中设置工作目录?

如果打开任意项目的属性,则可以更改任何目标项目的工作目录:

工作目录

但是,如果我尝试为项目打开单个属性表,则“调试”类别丢失,并且找不到任何可以更改的字段,这将允许我为该属性表设置工作目录(因此,请允许我同时更改多个项目的工作目录):

缺少“调试”类别

如何在属性表级别更改此属性?

c++ visual-studio visual-studio-2017

5
推荐指数
0
解决办法
411
查看次数

我什么时候需要交换功能?

当我写持有资源类,我很习惯写一个简单的交换功能,以简化传输/复印资源的过程.以下代码是一个简单的例子:

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)

c++ c++11

4
推荐指数
1
解决办法
169
查看次数