小编ksl*_*ksl的帖子

使用std :: move与std :: shared_ptr

我有一个定义如下的函数:

void foo(std::shared_ptr<X> x) { ... };
Run Code Online (Sandbox Code Playgroud)

如果我将共享ptr声明为X:

std::shared_ptr<X> sourcePtr(new X(...));
Run Code Online (Sandbox Code Playgroud)

然后我可以打电话foo如下:

foo(std::move(sourcePtr));
Run Code Online (Sandbox Code Playgroud)

要么

foo(sourcePtr);
Run Code Online (Sandbox Code Playgroud)

我明白,如果我使用第一个选项,则sourcePtr变为null.它是否也会阻止引用计数递增?

如果这无关紧要,我更喜欢哪个选项?做出这样的决定时,我应该考虑其他事情吗?

c++ shared-ptr move-semantics c++11

28
推荐指数
1
解决办法
2万
查看次数

与const std :: string的向量相关的编译失败

请有人解释为什么以下代码不能使用clang 3.5进行编译.

报告的错误是"算法中没有可行的重载"="."

std::vector<const std::string> m_messages;
std::vector<const std::string>::iterator iter;
...

if (iter != m_messages.end())
{
    m_messages.erase(iter);      // compilation error
}
Run Code Online (Sandbox Code Playgroud)

如果我声明m_messages为:std::vector<std::string> m_messages;那么它编译好了.

另外,有什么区别:

std::vector<const std::string> m_messages;
Run Code Online (Sandbox Code Playgroud)

std::vector<std::string> m_messages;
Run Code Online (Sandbox Code Playgroud)

TIA.

c++ iterator stdvector c++11

15
推荐指数
2
解决办法
1095
查看次数

报告了VS 2015中const vector <const T>的编译器错误,但未报告VS 2013

以下代码使用Visual Studio 2013编译正常.

#include <vector>
#include <string>

int main()
{
    const std::string constString("fred");
    const std::vector<const std::string> myVector{ constString };
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用Visual Studio 2015编译它,则会报告以下错误:

1>xmemory0(587): error C2338: The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.

我见过各种帖子,特别是这个帖子C++ 11允许vector <const T>吗?,关于vector<const T>以及为什么它不被允许,但我真的不明白.但是,在上面的例子中,向量本身是const.

有人可以解释一下吗?VS 2013成功编译是错误的吗?

const vector c++11 visual-studio-2013 visual-studio-2015

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

矢量std ::功能与不同的签名

我有许多具有不同签名的回调函数.理想情况下,我想将它们放在一个向量中,并根据某些条件调用适当的一个.

例如

void func1(const std::string& value);

void func2(const std::string& value, int min, int max);

const std::vector<std::function<void(std::string)>> functions
{
    func1,
    func2,
};
Run Code Online (Sandbox Code Playgroud)

我意识到上述情况是不可能的,但我想知道是否有任何我应该考虑的替代方案.我还没有找到任何东西,我已经尝试过,std::bind但没有成功实现我想要的东西.

这样的事情可能吗?

c++ stdvector c++11 std-function

14
推荐指数
2
解决办法
1万
查看次数

使用Visual Studio 2013(Express)构建强大功能

有没有人使用Visual Studio 2013 Express成功构建Boost?

正如Boost网站所述,我对此是否可行感到困惑:

Visual Studio 2013/Visual C++ 12的已知错误.

Visual Studio 2013在发布过程中很晚才发布,因此存在一些未解决的问题.这些包括:

由于缺少include,序列化无法编译.

使用Boost.Container的allocator_traits中的has_member_function_callable_with会导致编译错误(#9332).

在诸如Unordered和MultiIndex之类的库中,使用初始化程序列表调用重载函数会导致编译错误,Visual C++声称重载是不明确的.这是一个Visual C++错误,目前尚不清楚是否有一个好的解决方法.这不会影响不使用初始化程序列表的代码,也不会使用不需要隐式转换的初始化程序列表(即容器的确切值类型的初始化程序列表).

线程:ex_scoped_thread编译失败(#9333).

但是,这张海报说可以在没有任何补丁的情况下使用VS 2013构建Boost 1.55.0.

我曾尝试使用VS 2013构建Boost,但我确实至少得到了序列化错误.

c++ boost visual-studio-2013

12
推荐指数
1
解决办法
6317
查看次数

在Java中使用Thread.currentThread().join()

以下代码取自Jersey项目中的示例.看到这里.

public class App {

    private static final URI BASE_URI = URI.create("http://localhost:8080/base/");
    public static final String ROOT_PATH = "helloworld";

    public static void main(String[] args) {
        try {
            System.out.println("\"Hello World\" Jersey Example App");

            final ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class);
            final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false);
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    server.shutdownNow();
                }
            }));
            server.start();

            System.out.println(String.format("Application started.\nTry out %s%s\nStop the application using CTRL+C",
                    BASE_URI, ROOT_PATH));

            //////////////////////////////
            Thread.currentThread().join();
            //////////////////////////////

        } catch (IOException | InterruptedException ex) { …
Run Code Online (Sandbox Code Playgroud)

java multithreading jersey jersey-2.0

10
推荐指数
2
解决办法
4477
查看次数

将char数组中的元素范围提取到字符串中

我想从char数组的开头提取一系列元素并将它们放入一个字符串中.范围可以小于或等于元素的数量.

这就是我想出的.

// buffer is a std::array<char, 128>

std::string message;

for (int i = 0; i < numberToExtract; ++i)
{
    message += buffer.at(i);
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

我一直在寻找像std :: string的迭代器构造函数.例如, std::string(buffer.begin(), buffer.end())但我不希望所有的元素.

谢谢.

c++ arrays string c++11 stdarray

9
推荐指数
1
解决办法
6882
查看次数

什么是PDF抚摸,非抚摸和填充?

我刚刚开始使用Apache PDFBox,我完全不知道在应用于文本和行时,抚摸,非抚摸和填充是什么意思.

请有人指点我参考/指南,解释这些术语的含义(对于初学者)以及它们之间的区别.

pdf pdfbox

8
推荐指数
1
解决办法
3808
查看次数

使用braced-init初始化std :: shared_ptr <std :: map <>>

我有以下shared_ptr内容map:

std::shared_ptr<std::map<double, std::string>>
Run Code Online (Sandbox Code Playgroud)

我想用braced-init初始化它.可能吗?

我试过了:

std::string s1("temp");
std::shared_ptr<std::map<double, std::string>> foo = std::make_shared<std::map<double, std::string>>(1000.0, s1);
Run Code Online (Sandbox Code Playgroud)

但是在使用Xcode 6.3编译时会出现以下错误:

/usr/include/c++/v1/map:853:14: Candidate constructor not viable: no known conversion from 'double' to 'const key_compare' (aka 'const std::__1::less<double>') for 1st argument
Run Code Online (Sandbox Code Playgroud)

我已经尝试了第一个参数(1000.0)的其他变体而没有成功.

有人可以帮忙吗?

c++ stdmap shared-ptr c++11 list-initialization

8
推荐指数
1
解决办法
2038
查看次数

Xcode - 将二进制文件与调试和发布库链接起来

我在Xcode 5中有一个使用各种boost库的项目.我已经构建了boost的调试版和发行版,并将二进制文件放在同一个位置.

我想以这样的方式构建我的调试和发布应用程序,当我构建一个调试版本时,它链接到调试增强库,当我构建一个发行版本时,它链接到发布增强库.

在Xcode中,在Build Phases下,我没有看到如何在'Link binary With Libraries'中指定一组二进制文件用于调试,另一组用于发布.

我该怎么做呢?

c++ xcode linker boost

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