我已经阅读了 有关Stack Overflow RE的各种文章 :取消引用类型标记的指针错误。我的理解是,该错误本质上是编译器警告,它警告通过不同类型的指针访问对象的危险(尽管似乎为产生了例外),这是可以理解且合理的警告。 char*
我的问题特定于以下代码:为什么将指针的地址强制转换void**为此警告的合格字符(通过导致错误-Werror)?
而且,此代码针对多个目标体系结构进行了编译,只有其中一种会生成警告/错误-这是否暗示它合法地是特定于编译器版本的缺陷?
// main.c
#include <stdlib.h>
typedef struct Foo
{
int i;
} Foo;
void freeFunc( void** obj )
{
if ( obj && * obj )
{
free( *obj );
*obj = NULL;
}
}
int main( int argc, char* argv[] )
{
Foo* f = calloc( 1, sizeof( Foo ) );
freeFunc( (void**)(&f) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果上述我的理解是正确的,a void**仍然只是一个指针,这应该是安全的。
有没有不使用左值来解决此编译器特定警告/错误的解决方法?也就是说,我理解这以及为什么这可以解决问题,但是我想避免这种方法,因为我想利用 …
我遇到了一个我甚至不知道如何描述的编译错误!这完全让我感到困惑.
情况:
代码尝试使用用char*初始化的rvalue std :: string在堆栈上创建一个对象.
代码:
#include <iostream>
#include <string>
class Foo
{
public:
Foo(double d)
: mD(d)
{
}
Foo(const std::string& str)
{
try
{
mD = std::stod(str);
}
catch (...)
{
throw;
}
}
Foo(const Foo& other)
: mD(other.mD)
{
}
virtual ~Foo() {}
protected:
double mD;
};
class Bar
{
public:
Bar(const Foo& a, const Foo& b)
: mA(a)
, mB(b)
{
}
virtual ~Bar() {}
protected:
Foo mA;
Foo mB;
};
int main(int …Run Code Online (Sandbox Code Playgroud) 有人可以解释这个C++编译错误的本质吗?我正在涉及/学习重载全局运算符new,delete及其变体.我读了一对夫妇 的 文章 上 的 主题,但我无法找到一个似乎专门解决这个问题.
代码
foo.h:
#ifndef foo_h
#define foo_h
void* operator new(size_t);
void* operator new[](size_t);
void operator delete(void*);
void operator delete[](void*);
#endif // foo_h
Run Code Online (Sandbox Code Playgroud)
foo.cpp:
#include <foo.h>
#include <iostream>
void* operator new(size_t size) { return NULL; }
void* operator new[](size_t size) { return NULL; }
void operator delete(void* p) { }
void operator delete[](void* p) { }
Run Code Online (Sandbox Code Playgroud)
编译错误
>g++ -g -std=c++14 -I./ -c foo.cpp -o foo.o
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ext/new_allocator.h:33:0,
from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/x86_64-pc-cygwin/bits/c++allocator.h:33, …Run Code Online (Sandbox Code Playgroud) 我对bash变量是否导出到子shell以及何时可以通过脚本访问感到困惑.到目前为止,我的经验使我相信bash变量可自动用于子shell.例如:
> FOO=bar
> echo $FOO
bar
> (echo $FOO)
bar
Run Code Online (Sandbox Code Playgroud)
以上似乎表明bash变量可以在子壳中访问.
鉴于此脚本:
#! /usr/bin/bash
# c.sh
func()
{
echo before
echo ${FOO}
echo after
}
func
Run Code Online (Sandbox Code Playgroud)
我知道在当前shell上下文中调用脚本可以访问当前shell的变量:
> . ./c.sh
before
bar
after
Run Code Online (Sandbox Code Playgroud)
如果我在没有"点空间"先例的情况下调用脚本...
> ./c.sh
before
after
Run Code Online (Sandbox Code Playgroud)
...是不是在子shell中调用脚本的情况?如果是这样,并且当前shell的变量可用于子shell(正如我从最初的代码块推断的那样)也是$FOO如此,为什么c.sh在以这种方式运行时不可用?
类似地,为什么在括号内运行$FOO时也不可用c.sh- 我理解为在子shell中运行表达式:
> (./c.sh)
before
after
Run Code Online (Sandbox Code Playgroud)
(如果这个问题没有太多问题:如果" ./c.sh"和" (./c.sh)"都在当前shell的子shell中运行脚本,那么两种调用方式之间有什么区别?)
从这次试验来看,Docker 的命令似乎COPY没有保留符号链接——相反,它“遵循”符号链接并复制目标文件(?):
$ ls -l
total 4
lrwxrwxrwx 1 user domain users 1 Mar 26 09:37 a -> b
-rw-r--r-- 1 user domain users 0 Mar 26 09:37 b
lrwxrwxrwx 1 user domain users 1 Mar 26 09:41 c -> d
-rw-r--r-- 1 user domain users 0 Mar 26 09:41 d
-rw-r--r-- 1 user domain users 54 Mar 26 09:39 Dockerfile
$
Run Code Online (Sandbox Code Playgroud)
# Dockerfile
FROM alpine:3.7 as base
COPY [ "./*", "/foo/bar/" ]
Run Code Online (Sandbox Code Playgroud)
$ docker build -t foo:tmp …Run Code Online (Sandbox Code Playgroud) 更新:这个问题是用 MVRE 重构的。
\n有没有办法可以RUN从使用 构建的 Dockerfile 中查看完整命令docker build?\n例如 如果我的 Dockerfile 有这样的语句:
# Dockerfile\nFROM alpine:3.7 as base\nRUN echo "this is the song that doesn\'t end. Yes it goes on and on, my friends. Some people started singing it not knowing what it was, and they\'ll continue singing it forever just because..."\nRun Code Online (Sandbox Code Playgroud)\n...有没有办法可以看到完整的命令,即echo "this is the song that doesn\'t end. Yes it goes on and on, my friends. Some people started singing …
我们的 gitlab CI/CD Web 界面允许我们在成功运行后下载“工件”(例如可执行文件、共享库),如下面的屏幕截图所示:
问题:有没有办法从 shell 中下载该工件? (首选使用curl的解决方案)
“ Copy Link Location”右键菜单选项给了我类似的东西https://git.my_company.com/namespace/project/-/jobs/987134/artifacts/download,所以我尝试了以下操作:
$ curl -O -L https://git.my_company.com/namespace/project/-/jobs/987134/artifacts/download
Run Code Online (Sandbox Code Playgroud)
但这并没有下载正确的东西:它让我呕吐了 html,但我期待一个 .zip 文件。
如果我尝试从 Web UI 下载文件,文件名默认为artifacts.zip,所以我也尝试过:
$ curl -O -L https://git.my_company.com/namespace/project/-/jobs/987134/artifacts/download/artifacts.zip
Run Code Online (Sandbox Code Playgroud)
这确实创建了一个名为 的文件artifacts.zip,但它仍然不是预期的文件:它仍然是 html,尽管与第一次描述的尝试不同。
我想请求帮助正确的语法来声明一个std :: map,其mapped_type是模板类的内部类.
请在下面的代码中找到#if /#else块."#if 1"块具有包含内部类Inner的模板类Outer.外部定义了函数Func,它采用了一个std映射,其mapping_type是Inner类型.
#include <map>
#if 1
template<typename C, typename T>
class Outer
{
public:
Outer(const C& c, const T& t){}
virtual ~Outer(){}
class Inner
{
public:
Inner(){}
Inner(T t){}
virtual ~Inner(){}
protected:
T mT;
};
void Func(std::map<C, Inner>& rMap);
protected:
std::map<C, Inner> mMap;
};
template<typename C, typename T>
void Outer<C, T>::Func(std::map<C, Outer::Inner>& rMap)
{
std::map<C, Inner>::iterator iter;
for (iter = rMap.begin(); iter != rMap.end(); ++iter)
{
mMap[iter->first] = iter->second;
}
}
#else
class Outer
{
public: …Run Code Online (Sandbox Code Playgroud) 谁能帮助解释这种意想不到的行为?
前提
我创建了包含成员std::thread变量的类Thread .Thread的ctor构造成员,std::thread提供指向静态函数的指针,该函数调用纯虚函数(由基类实现).
代码
#include <iostream>
#include <thread>
#include <chrono>
namespace
{
class Thread
{
public:
Thread()
: mThread(ThreadStart, this)
{
std::cout << __PRETTY_FUNCTION__ << std::endl; // This line commented later in the question.
}
virtual ~Thread() { }
static void ThreadStart(void* pObj)
{
((Thread*)pObj)->Run();
}
void join()
{
mThread.join();
}
virtual void Run() = 0;
protected:
std::thread mThread;
};
class Verbose
{
public:
Verbose(int i) { std::cout << __PRETTY_FUNCTION__ << ": " << i …Run Code Online (Sandbox Code Playgroud) 这是Can C++ 11的后续条件condition_variables用于同步进程?.
std :: condition_variable对象可以用作计数信号量吗?
Methinks不是因为对象似乎绑定到std :: mutex,这意味着它只能用作二进制信号量.我在网上看过,包括这里,这里和这里,但是找不到使用这些对象作为计数信号量的参考或示例.
c++ ×5
c++11 ×2
docker ×2
bash ×1
c ×1
casting ×1
constructor ×1
copy ×1
curl ×1
dictionary ×1
docker-build ×1
dockerfile ×1
gcc ×1
git ×1
gitlab ×1
inheritance ×1
pointers ×1
pure-virtual ×1
semaphore ×1
shell ×1
std ×1
stdstring ×1
stdthread ×1
stl ×1
subprocess ×1
subshell ×1
symlink ×1
templates ×1