我正在使用环境值$ PATH.我发现$ PATH包含/ snap/bin目录,它不存在.路径有什么用?我可以从$ PATH中删除它还是应该将其删除?请给我你的建议.非常感谢你?
我想将函数的扩充打印到文件中.我被告知命令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函数的原始扩充.
为什么输出和原始增量不同?或者我可以打印出原始增益吗?
非常感谢你.
我试图将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)
非常感谢你.
我在学习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)
我想这是因为空函数的定义.我想知道为什么我不能定义一个空函数?
我想知道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) * 我之前问过一个问题,但这不是正确的问题。现在我提出了正确的问题并修复了示例代码。我将提出一个答案,其中部分引用了上一个问题的答案。*
我想在 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)
非常感谢。
在 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) 我找到了一个获取“文件”文件描述符的代码。代码如下。
exec {fd}<'file'
Run Code Online (Sandbox Code Playgroud)
不幸的是,代码没有提供为什么它可以通过exec获取文件描述符。即使我阅读了 bash 的手册,我也无法理解 exec 在这里执行什么命令。如果有人知道上面代码的机制,请告诉我。非常感谢。
我正在学习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>.
非常感谢你.
我想mkdir在python中使用。
它有一个参数mode,其默认值为'0o777'。
我知道0777Linux 中的文件模式。但是我不知道和o之间是什么。它是什么?0777