小编use*_*460的帖子

Noexcept和copy,移动构造函数

在我看来,似乎是协议,当移动构造函数为noexcept(false)时,标准库必须调用复制构造函数而不是移动构造函数.

现在我不明白为什么会这样.此外,Visual Studio VC v140和gcc v 4.9.2似乎也有不同的做法.

我不明白为什么除了这是例如矢量的关注.我的意思是如果T没有,vector :: resize()应该如何能够提供强大的异常保证.正如我所看到的那样,矢量的异常级别将取决于T.无论是否使用复制或移动.我理解noexcept只是对编译器进行异常处理优化的眨眼.

当使用Visual Studio编译时,这个小程序在使用gcc编译时调用复制构造函数并移动构造函数.

include <iostream>
#include <vector>

struct foo {
  foo() {}
  //    foo( const foo & ) noexcept { std::cout << "copy\n"; }
  //    foo( foo && ) noexcept { std::cout << "move\n"; }
  foo( const foo & )  { std::cout << "copy\n"; }
  foo( foo && )  { std::cout << "move\n"; }

  ~foo() noexcept {}
};

int main() {
    std::vector< foo > v;
    for ( int i = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ noexcept c++11

11
推荐指数
2
解决办法
3851
查看次数

停止 PowerShell 命令

当 PowerShell 命令开始执行时,如何停止它?

例如,我如何停止这样的命令

Select-String -Path c:\*.* "Cat"
Run Code Online (Sandbox Code Playgroud)

无需终止我的 PowerShell 终端?

Ctrl-CBreak不起作用。

powershell

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

如何使用 std::ranges 加入视图

如何使用 std::ranges 连接两个视图?
在ranges-v3中,视图与views::concat()连接,我不知道如何使用std::ranges来做到这一点。

#include <string>
#include <iostream>
#include <vector>
#include <ranges>
using namespace std;
using namespace views;

void snake_to_camel() {   
    auto transform = views::transform;
    auto const s = string{"feel_the_force"};
    auto words = s | split('_');                        // [[f,e,e,l],[t,h,e],[f,o,r,c,e]]
    auto words_cap = words | transform([](auto word){
        auto transform = views::transform;
        auto head = word | take(1) 
                         | transform([](unsigned char c){return toupper(c);}); // e.g. [F]
        auto tail = word | drop(1);                     // e.g. [e.e.l]
        return tail;
        //return concat( head, tail );                  // …
Run Code Online (Sandbox Code Playgroud)

c++ c++20 std-ranges

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

拆分参数包

我想拆分模板参数包。像这样的东西。我怎么能去做这件事?

template< typename... Pack >
struct TypeB : public TypeA< get<0, sizeof...(Pack)/2>(Pack...) >
             , public TypeA< get<sizeof...(Pack)/2, sizeof...(Pack)>(Pack...) > 
{
};
Run Code Online (Sandbox Code Playgroud)

这是我对为什么这个问题不重复的看法:我正在寻找一种通用的方法来做到这一点,当将拆分包传递给其他模板类时,该方法将起作用 - 如上所示。以及将其传递给函数。

c++ templates template-inheritance template-meta-programming variadic-templates

5
推荐指数
1
解决办法
134
查看次数

如何获取参数包中的元素索引

如何获取以下内容以将参数包元素的索引放入元组中?

template< typename... Ts >
class ClassA {
public:
    ClassA( Ts... ts ) : tup( make_tuple( ts, 0 )... ) {}
    // I would like it to expand to this.
    // ClassA( T0 ts0, T1 ts1 ) : tup( make_tuple( ts0, 0 ), make_tuple(ts1, 1) ) {}
    tuple<tuple<Ts, size_t>...> tup;
};

void main() {
    vector<int> a ={ 2, 4, 5 };
    list<double> b ={ 1.1, 2.2, 3.3 };
    ClassA<vector<int>, list<double>, vector<int>, list<double>> mm( a, b, a, b );
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

c++ templates template-meta-programming variadic-templates c++17

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

完美转发需要衰减

我不明白为什么以下代码无效。

#include <type_traits>
#include <tuple>

template<typename... Ts>
void funka( std::tuple<Ts...>&& v ) {    
}

template<typename T>
void funkb( T&& v ) {
    funka( std::forward<T>( v ) );
}

void funk() {
    auto tup = std::tuple<int,int>( 1, 2 );
    funkb( tup );
}
Run Code Online (Sandbox Code Playgroud)

它失败并出现此错误:

<source>: In instantiation of 'void funkb(T&&) [with T = std::tuple<int, int>&]':
<source>:24:16:   required from here
<source>:10:10: error: cannot bind rvalue reference of type 'std::tuple<int, int>&&' to lvalue of type 'std::tuple<int, int>'
     funka( std::forward<T>( v ) );
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ …
Run Code Online (Sandbox Code Playgroud)

c++ perfect-forwarding

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

用户定义的容器不适用于 std::ranges

我无法让用户定义的容器与 std::ranges 一起使用。如果迭代器只是一个,我的容器就可以工作,int*但是一旦我创建了自己的迭代器类,就会出现编译器错误。

这有效。

#include <iostream>
#include <ranges>
using namespace std;

struct sentinel {};

class my_iota_view_a : public std::ranges::view_interface< my_iota_view_a >{
public:
    using value_type        = size_t;
    using iterator          = value_type *;

    my_iota_view_a() = default;
    my_iota_view_a( size_t begin ) : mBegin( &mData[begin] ) { }

    iterator begin() { return mBegin; }
    sentinel end() { return sentinel{}; }
private:
    value_type mData[10] ={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    value_type * mBegin;
};

bool operator==( my_iota_view_a::iterator …
Run Code Online (Sandbox Code Playgroud)

c++ c++20 std-ranges

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