小编Ted*_*gmo的帖子

检查 bash 中数组是否为空

我想看看 bash 中的数组是否为空

key=[]
key1=["2014"]
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

[[ -z "$key" ]] && echo "empty" || echo "not Empty" 
[[ -z "$key1" ]] && echo "empty" || echo "not Empty"
Run Code Online (Sandbox Code Playgroud)

两者都返回“非空”

[[ $key==[] ]] && echo "empty" || echo "not Empty"
[[ $key1==[] ]] && echo "empty" || echo "not Empty"
Run Code Online (Sandbox Code Playgroud)

两人皆归empty

arrays bash if-statement

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

为什么半初始化结构的 constinit 不起作用

struct A1 { int x;     int y; };
struct A2 { int x = 1; int y = 2; };
struct A3 { int x = 1; int y; };

constinit A1 a1; // x == 0, y == 0.
constinit A2 a2; // x == 1, y == 2.
          A3 a3; // x == 1, y == 0.
constinit A3 a4; // Error: illegal initialization of 'constinit' entity with a non-constant expression

int main() {}
Run Code Online (Sandbox Code Playgroud)

有什么问题a4吗?我的意思a4.x …

c++ language-lawyer c++20

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

std 类专业化 - 满足原始 std::array 模板的标准库要求

在为std::array 定义专门化是否安全std::array中,提问者询问专门化具有昂贵的默认构造函数的程序定义类型是否安全。目标是std::array使用廉价的方法来初始化 中的所有元素构造函数而不是默认构造函数来初始化 中的所有元素。

我现在并不质疑这个设计。我很好奇这种可能性,对于这个问题,我建议在聚合初始化时使用大括号省略,定义一个内部类并通过标签使用特殊的构造函数:

namespace foo {
struct cheap_tag_t {};
inline constexpr cheap_tag_t cheap_tag;

class bar {
public:
    inline bar() { std::cout << "expensive ctor\n"; }
    inline bar(int) { std::cout << "int ctor\n"; }
    inline bar(cheap_tag_t) { std::cout << "cheap ctor\n"; }
};
}  // namespace foo
Run Code Online (Sandbox Code Playgroud)
template <std::size_t N>
    requires (N != 0)     // let the standard array take care of N==0
struct std::array<foo::bar, N> {
    struct inner_array { …
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization language-lawyer stdarray

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

将左值包装在双花括号中是否会创建一个可以作为右值引用传递的临时对象?

以下代码在几种不同的编译器上进行编译,包括 g++、clang 和 MSVC,但我不明白为什么:

#include <string>

class C {
    std::string m;
};

void Accept(C &&);

extern C c;

int main() {
    Accept({{c}});
}
Run Code Online (Sandbox Code Playgroud)

生成的汇编代码似乎显示在调用 Accept() 之前对 std::string 的复制构造函数的调用,我假设这意味着编译器生成了对类 C 的复制构造函数的调用。因此,使用{{c}}作为右值引用参数的参数似乎会创建一个临时副本。

这是准确的解释吗?如果是这样,哪些 C++ 语言功能组合起来可以{{c}}创建 的临时副本c

c++ language-lawyer

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

类似聚合的初始化,无需构造函数的多余调用

我主要尝试编写std::array. 但是,我对聚合初始化的规则(以及其他一些小细节)不太满意,因此不想使用它。

我不喜欢聚合初始化的事情:

  • 列表中少于必要的元素不会产生错误,这对于聚合的聚合来说尤其奇怪,例如
    struct A { int x,y; };
    std::array< A, 2 > arr{1,2,3};
    // produces A[0].x = 1, A[0].y = 2, A[1].x = 1, A[1].y = 0
    
    Run Code Online (Sandbox Code Playgroud)
  • 用户仍然可以调用私有/已删除的 ctor
    class B { B() = delete; };
    B{};  // no compile time error
    
    Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的是以下内容:

#include <cstddef>
#include <type_traits>
#include <utility>

template< typename T, typename ... Ts >
inline constexpr bool areT_v = std::conjunction_v< std::is_same<T, Ts> ... >;  

namespace ttt {
    template< typename T, int N >
    struct …
Run Code Online (Sandbox Code Playgroud)

c++ aggregate-initialization c++14

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

避免在析构函数中无限递归

作为我的大学分配给我的练习的一部分,我编写了一个小的 Graph 实现,遵循这个标题。

class Node {

private:
    std::string name;
    std::vector<Node*> children;

public:
    Node(const std::string& name="");
    virtual ~Node();

}
Run Code Online (Sandbox Code Playgroud)

在为析构函数编写代码时~Node(),我注意到当图形包含循环时我的实现失败。到目前为止,这是我的实现,如果图形包含循环,这显然不起作用。

Node::~Node() {
    for (Node* n : children) {
        delete n;
        n = NULL;
    }
    children.clear();
}
Run Code Online (Sandbox Code Playgroud)

我不确定如何最优雅地编写一个可以处理图中循环的析构函数?

请注意,我的任务是专门编写一个递归析构函数。谢谢您的回答!

c++ recursion destructor graph c++14

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

重载运算符上的函数调用不正确

在代码中,为什么是(10 != i)calling==而不是!=?另外两个打电话!=

#include <iostream>

class Integer
{
  int x;

public:
  bool
  operator== (const Integer &i)
  {
    std::cout << "==";
    return x == i.x;
  }
  bool
  operator!= (const Integer &i)
  {
    std::cout << "!=";
    return x != i.x;
  }
  Integer (int t = 0) { x = t; }
};

int
main ()
{
  Integer i;
  std::cout << (i != i) << '\n';   // calls !=
  std::cout << (i != 100) << '\n'; …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading c++20

7
推荐指数
2
解决办法
230
查看次数

为什么我不能像这样将函数与右值参数绑定?

#include <functional>
#include <iostream>
#include <string>
#include <utility>
using namespace std;

template<typename Func, typename... Args>
void do_task(Func &func, Args &&...args)
{
    auto f = bind(func, forward<Args>(args)...);
    f();
}

int main()
{
    string name = "hello";
    auto test_func = [](string name, string &&arg1, string &&arg2) -> void {
        cout << name << " " << arg1 << " " << arg2 << endl;
    };

    // do_task(test_func,"test_one", "hello", string("world"));     // compile error
    // do_task(test_func, "test_tww", std::move(name), "world");    // compile error
    do_task(test_func, "test_three", …
Run Code Online (Sandbox Code Playgroud)

c++ templates rvalue-reference stdbind stdmove

7
推荐指数
0
解决办法
75
查看次数

如何声明一个具有 N 个 double 类型参数的函数?

我想要一个能够存储接受“N”个双参数的功能对象的类模板。此伪代码使用一个不存在的std::repeated_type函数模板来解决问题并说明预期用法:

template<int N>
class FunctionHolder {
public:
   using function_type = std::function<int(std::repeated_type<double, N> args)>;
   FunctionHolder(const function_type& arg): m_func(arg) {}
private:
   const function_type& m_func;
};

int my_func(double arg1, double arg2);

void main() {
   FunctionHolder<2> my_holder(my_func);  
}
Run Code Online (Sandbox Code Playgroud)

我希望代码尽可能简单且可读,因此即使我模糊地理解我可以使用 std::integer_sequence 和辅助类模板来缝合解决方案,但我不相信我的解决方案足够简单。

c++ template-meta-programming

7
推荐指数
4
解决办法
470
查看次数

当混合成员和非成员二元运算符时,clang 是否错误地报告了歧义?

考虑以下代码,其中混合了成员和非成员operator|

template <typename T>
struct S
{
    template <typename U>
    void operator|(U)
    {}
};

template <typename T>
void operator|(S<T>,int) {} 

int main()
{
    S<int>() | 42;
}
Run Code Online (Sandbox Code Playgroud)

在 Clang 中,此代码无法编译,说明对的调用operator|不明确,而 gcc 和 msvc 可以编译它。我希望选择非成员超载,因为它更专业。标准规定的行为是什么?这是 Clang 中的错误吗?

(请注意,将运算符模板移到结构之外可以解决歧义。)

c++ operator-overloading clang language-lawyer clang++

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