小编tem*_*boy的帖子

为什么使用此删除的功能有效?

struct S {
    S() {}
    S (const S &) = delete;
};

void f1 (S s) {}
void f2 (S &s) {}

int main() {
    S s;

    f2(s);
}
Run Code Online (Sandbox Code Playgroud)

既然S(S &s)删除了,为什么使用f2不抛出错误,因为它被声明它传递参数S &s?当我使用f1(s)它时会抛出错误.我已经看过删除函数的定义,我认为这会引发错误,但事实并非如此.为什么?

c++

4
推荐指数
2
解决办法
179
查看次数

找不到符号IllegalArgumentException

我有一个非常简单的程序试图抛出异常.编译器说它无法找到,IllegalArgumentException即使我把它放在throw说明符部分时它没有说出任何关于该名称的内容:

import java.lang.*;

class A
{
    public A() throws IllegalArgumentException
    {
        if (n <= 0)
            throw IllegalArgumentException("n is less than 0");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

Main.java:28: error: cannot find symbol
            throw IllegalArgumentException("n is less than 0");
                  ^
  symbol:   method IllegalArgumentException(String)
  location: class A
1 error
Run Code Online (Sandbox Code Playgroud)

我意识到这很简单(我第一次尝试编写Java).我试过寻找答案,但他们没有帮我解决问题.

java

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

什么是"提示"?

cppreference有关map::emplace_hint():

template <class... Args>
iterator emplace_hint( const_iterator hint, Args&&... args );
Run Code Online (Sandbox Code Playgroud)

将新元素插入到容器中,使用hint元素应该作为建议.

所以,如果它是一个建议,那是否意味着实现不具备的元素有什么地方呢?究竟是什么提示?

c++ c++11

4
推荐指数
2
解决办法
890
查看次数

为什么有些线程被推迟了?

在我关注的教程中,作者编写了一个程序,该程序显示std::futures 的析构函数并不总是执行任务.在下面的程序中,创建的10个线程std::async()被移动到向量中,然后我们等待它们的析构函数运行.

#include <iostream>
#include <future>
#include <thread>
#include <chrono>

int main()
{
    std::cout << "Main thread id: " << std::this_thread::get_id() << std::endl;

    std::vector<std::future<void>> futures;
    for (int i = 0; i < 10; ++i)
    {
        auto fut = std::async([i]
        {
            std::this_thread::sleep_for(std::chrono::seconds(2));
            std::cout << std::this_thread::get_id() << " ";
        });
        futures.push_back(std::move(fut));
    }
}
Run Code Online (Sandbox Code Playgroud)

结果取决于机器,但我们发现在析构函数运行时只启动了6个线程(我们在主线程id输出后只打印了6个ID).这意味着其他四个被延迟,并且延迟线程在std::future析构函数期间不会运行.

我的问题是为什么有些线程被迫执行而其他线程被推迟.如果生命std::future结束,那么推迟他们的意义何在?

c++ multithreading c++11

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

理解左值到右值转换的示例

我很难理解这段代码(C++ 14草案标准[conv.lval]中的一个例子)是如何调用未定义的行为的g(false).为什么constexpr让程序有效?

另外,"不访问y.n" 是什么意思?在两个调用中g()我们都返回n数据成员,为什么最后一行说它不访问它?

struct S { int n; };
auto f() {
    S x { 1 };
    constexpr S y { 2 };
    return [&](bool b) { return (b ? y : x).n; };
}
auto g = f();
int m = g(false); // undefined behavior due to access of x.n outside its
                  // lifetime
int n = g(true);  // OK, does not access y.n
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer lvalue-to-rvalue constexpr c++14

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

参数列表中的void_t有效,但不作为返回类型

关于使用别名的cppreference有一个例子.此示例失败,因为int没有成员foo:

template<typename...> using void_t = void;
template<typename T> void_t<typename T::foo> f();
f<int>(); // error, int does not have a nested type foo
Run Code Online (Sandbox Code Playgroud)

这很清楚,但是当我尝试将该void_t部件放入参数列表时,它意外地编译:

template<typename...> using void_t = void;
template<typename T> void f(void_t<typename T::foo>);
f<int>();
Run Code Online (Sandbox Code Playgroud)

它在clang上编译但在gcc中没编译.这是一个错误吗?

c++ templates c++11 c++14

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

为什么内联函数不能看到内部范围?

我知道内联函数是将其主体插入到调用它们的位置的函数.那么为什么调用它们时不会受范围更改影响的内联函数:

#include <iostream>

inline void alert(const std::string &str) { cout << str; }

int main() {
    using namespace std;

    alert("Hello World"); // cout << "Hello World";
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为我得到错误cout was not declared in this scope,但如果我这样std::cout做.如果内联函数的函数体插入到作用域中,为什么C++不知道它cout是否是成员std

c++

3
推荐指数
1
解决办法
165
查看次数

为什么在未评估的操作数内部的包扩展会导致最后一个元素?

我可以在里面做到这一点decltype():

auto g() -> decltype(1, "", true, new int);
Run Code Online (Sandbox Code Playgroud)

但不是这个:

template <class... Args>
auto g(Args&&... args) -> decltype(args...);
Run Code Online (Sandbox Code Playgroud)

它失败了,因为包内扩展出现在里面,decltype()但我认为包扩展会导致以逗号分隔的参数列表.因此返回类型g(a, b, c)将是decltype(c)因为逗号运算符的工作原理(它返回最后一个元素).当你在函数参数列表,模板参数列表,初始化列表等内部扩展时,它可以工作.但为什么这不是这种情况?

c++ templates comma-operator variadic-templates c++11

3
推荐指数
1
解决办法
125
查看次数

线程没有被分离

我正在尝试创建一个线程池.我有一个std::unordered_map将线程ID映射到std::threads(workers).线程ID是等待将任务推入线程池(waiters)的线程的ID .任务由std::stack(tasks)表示.将任务推入池后,任务将从堆栈中弹出,并成为线程函数,作为映射的"值"部分.

在析构函数中,我尝试分离仍在运行的所有线程.但我仍然得到以下异常:

terminate called without an active exception
bash: line 7: 16881 Aborted                 (core dumped) ./a.out
Run Code Online (Sandbox Code Playgroud)

这意味着线程没有分离,程序终止.但是我的析构函数会遍历元素并将它们分离(我相信).为什么会发生这种情况,我该如何解决?

#include <queue>
#include <stack>
#include <mutex>
#include <thread>
#include <algorithm>
#include <functional>
#include <type_traits>
#include <unordered_map>
#include <condition_variable>

template <class F>
class thread_pool
{
    static_assert(std::is_function<F>::value, "F must have function type");
public:
    thread_pool();
    ~thread_pool();
    template <class Task>
    void push(Task&&);
private:
    std::unordered_map<std::thread::id, std::thread> workers;
    std::queue<std::thread> waiters;
    std::stack<std::function<F>> tasks;
    static std::size_t max;
private: …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

3
推荐指数
1
解决办法
174
查看次数

隐式使用initializer_list

§[dcl.init.list] 8.5.4/2:

模板std::initializer_list未预定义; 如果在<initializer_list>使用之前未包含标题std::initializer_list- 即使是未命名类型的隐式用法(7.1.6.4) - 程序也是格式错误的.

这是否意味着这个程序是不正确的?

#include <vector>
int main() {
    // uses vector::vector(initializer_list<T>, const Allocator&) constructor
    std::vector<int> v = {1, 2, 3};
}
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++11

3
推荐指数
1
解决办法
228
查看次数