小编mor*_*ora的帖子

$ PATH中的/ snap/bin目录是什么?我可以从$ PATH中删除吗?

我正在使用环境值$ PATH.我发现$ PATH包含/ snap/bin目录,它不存在.路径有什么用?我可以从$ PATH中删除它还是应该将其删除?请给我你的建议.非常感谢你?

linux ubuntu

17
推荐指数
1
解决办法
9392
查看次数

如何在bash中使用printf"%q"?

我想将函数的扩充打印到文件中.我被告知命令printf"%q",其指令如下,

# man printf
%q     ARGUMENT is printed in a format that can be reused as shell input, escaping non-print?
          able characters with the proposed POSIX $'' syntax.
Run Code Online (Sandbox Code Playgroud)

在上面的说明的基础上,我尝试了以下代码.

#!/bin/bash
# file name : print_out_function_augs.sh

output_file='output.txt'

function print_augs() {
  printf "%q " "$@" >> "${output_file}"
  echo >> "${output_file}"
}

print_augs a 'b c'

cat "${output_file}"
rm "${output_file}"
Run Code Online (Sandbox Code Playgroud)

并运行

bash print_out_function_augs.sh
Run Code Online (Sandbox Code Playgroud)

结果如下,

a b\ c
Run Code Online (Sandbox Code Playgroud)

我期待结果为

a 'b c'
Run Code Online (Sandbox Code Playgroud)

这是print_augs函数的原始扩充.

为什么输出和原始增量不同?或者我可以打印出原始增益吗?

非常感谢你.

bash

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

构造函数中的成员函数指针

我试图将std :: shared_pointer与删除器一起使用.我试图使用成员函数作为删除器.但它无法编译.编译器给了我一个错误消息,但我无法理解为什么它不起作用.有谁知道它为什么不起作用?非常感谢你.

简化的代码如下,

#include <memory>

class MemberFunctionPointerInConstructor {
public:
    MemberFunctionPointerInConstructor(void) {
        std::shared_ptr<int> a = std::shared_ptr<int>(new int(1), deleter);  // this line makes a compiler error message
    }

    void deleter(int* value) {
        delete value;
    }
};
Run Code Online (Sandbox Code Playgroud)

编译器的错误消息如下,

error: invalid use of non-static member function
std::shared_ptr<int> a = std::shared_ptr<int>(new int(1), deleter);
                                                                 ^
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

c++

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

为什么我不能在shell中定义一个空函数?

我在学习bash.我不小心遇到了空函数的语法错误.

#!/bin/bash
# script name : empty_function.sh
function empty_func() {
}

bash empty_function.sh

empty_function.sh: line 3: syntax error near unexpected token `}'
empty_function.sh: line 3: `}'
Run Code Online (Sandbox Code Playgroud)

我想这是因为空函数的定义.我想知道为什么我不能定义一个空函数?

bash shell

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

c ++是否像奇怪的反复出现的模板模式一样保证将祖母基类降级为盛大的子类?

我想知道c ++是否保证将祖母基类降级为盛大的子类,就像奇怪的重复模板模式一样.以下代码在我的环境中运行良好.但是我不确定它是否适用于任何条件/环境.请告诉我你的了解.非常感谢你.

PS.老实说,我不确定我的方式要求堆栈溢出是好还是坏.如果您发现了坏点,请告诉我.再次感谢你.

#include <iostream>

template <typename GrandChild>
class GrandBase {
public:
    void print(void) {
        const GrandChild* grand_child = static_cast<const GrandChild*>(this);
        std::cout << "GrandChild::value = " << grand_child->grand_child_value << std::endl;
    }
};

template <typename Derived>
class BridgeClass : public GrandBase<Derived> {};

class GrandChild : public BridgeClass<GrandChild> {
public:
    GrandChild() = default;
    virtual ~GrandChild() = default;

    int grand_child_value = 17;
};

void test_down_casting_to_grand_base(void) {
    GrandChild a;
    a.print();
}

int main(int argc, char **argv) {
    test_down_casting_to_grand_base();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

GrandChild::value = …
Run Code Online (Sandbox Code Playgroud)

c++ downcast

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

我可以在 bash 中为数组分配默认值吗?

* 我之前问过一个问题,但这不是正确的问题。现在我提出了正确的问题并修复了示例代码。我将提出一个答案,其中部分引用了上一个问题的答案。*

我想在 bash 中将默认值设置为数组。请看下面,

function chmod_chown_func() {
  local file_path="$1"
  local chmod_options[2]=${2:='-R 744'}  # This line has error.
  local chown_options[2]=${3:='-R root:root'}  # This line has error.
  sudo chmod "${chmod_options[@]}" "${file_path}"
  sudo chown "${chown_options[@]}" "${file_path}"
}

chmod_chown_func "test.txt"
Run Code Online (Sandbox Code Playgroud)

错误信息是

$2: cannot assign in this way
Run Code Online (Sandbox Code Playgroud)

非常感谢。

bash

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

namerefs (declare -n) 可以与 bash 中的数组一起使用吗?当它说声明 -n 不能应用于数组时,文档是什么意思?

在 stackoverflow 的另一个问题中,我询问了如何将数组传递给函数。一个答案向我推荐了以前的答案。一个答案表明,使用 -n 选项声明,引用,对于将数组传递给函数很有用,如下所示,

declare -a array=( 1 2 )

function array_pass_by_reference_func() {
  local -n aug=${1:-dummy}
  echo "pass by reference : array[0] = ${aug[0]}"
  echo "pass by reference : array[1] = ${aug[1]}"
  echo "pass by reference : array = ${aug[@]}"
}

# execute an example function above
array_pass_by_reference_func "array"

# output
pass by reference : array[0] = 1
pass by reference : array[1] = 2
pass by reference : array = 1 2
Run Code Online (Sandbox Code Playgroud)

它看起来运作良好。我的问题是关于 bash 手册中声明 -n 的指令,

declare …
Run Code Online (Sandbox Code Playgroud)

bash

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

为什么exec {fd}&lt;file将file的文件描述符分配给fd?

我找到了一个获取“文件”文件描述符的代码。代码如下。

exec {fd}<'file'
Run Code Online (Sandbox Code Playgroud)

不幸的是,代码没有提供为什么它可以通过exec获取文件描述符。即使我阅读了 bash 的手册,我也无法理解 exec 在这里执行什么命令。如果有人知道上面代码的机制,请告诉我。非常感谢。

bash

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

如何让编译器推导出一种nullptr?

我正在学习c ++.我想让编译器将nullptr推导为shared_ptr.请阅读以下代码,

struct A {};

struct B {
    std::shared_ptr<A> a;
};

struct Tag {
    std::shared_ptr<B> b;
};

auto GetSharedPtrClassB(Tag* tag) {
    if (tag) {
        auto& sharedB = *(tag->b);
        return sharedB.a;
    } else {
        return nullptr;  // Error : compiler cannot deduce type of nullptr.
    }
}
Run Code Online (Sandbox Code Playgroud)

GetSharedPtrClassB,nullptr不能推断为std::shared_ptr<A>.错误消息如下,

error: inconsistent deduction for ‘auto’: ‘std::shared_ptr<A>’ and then ‘std::nullptr_t’
Run Code Online (Sandbox Code Playgroud)

我怎样才能让编译器推导出nullptr为std::shared_ptr<A>?我可以提供一种类型decltype(*(tag->b)),但我想不出提供类型的下一步std::shared_ptr<A>.

非常感谢你.

c++

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

0o777 作为 mkdir 模式值的含义是什么?

我想mkdir在python中使用。

它有一个参数mode,其默认值为'0o777'

我知道0777Linux 中的文件模式。但是我不知道和o之间是什么。它是什么?0777

python literals octal

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

标签 统计

bash ×5

c++ ×3

downcast ×1

linux ×1

literals ×1

octal ×1

python ×1

shell ×1

ubuntu ×1