我想知道是否有任何理由更喜欢private(var)OpenMP中的子句而不是(私有)变量的本地定义,例如
int var;
#pragma omp parallel private(var)
{
...
}
Run Code Online (Sandbox Code Playgroud)
与
#pragma omp parallel
{
int var;
...
}
Run Code Online (Sandbox Code Playgroud)
另外,我想知道私人条款的重点是什么.这个问题已在OpenMP中解释过:局部变量是否自动私有?,但我确信答案是错的 我不喜欢答案,因为即使C89不阻止你在函数中间定义变量,只要它们在作用域的开头(这是自动的输入并行区域的情况).因此,即使对于老式的C程序员来说,这也不应该有任何区别.我是否应该将其视为一种语法糖,它允许在过去的好日子中使用"定义变量 - 在你的功能中开始"的风格?
顺便说一句:在我看来,第二个版本也阻止程序员在并行区域之后使用私有变量,希望它可能包含一些有用的东西,所以另一个-1用于private子句.
但是因为我对OpenMP很陌生,所以如果没有对它的解释,我不想怀疑它.提前谢谢你的答案!
我正在尝试libgit2使用 SSH 密钥对 git 服务器进行身份验证。到目前为止,这适用于像 的 URL ssh://myuser@host.domain:1234/dirs/repo.git,其中我的应用程序接受 URL 作为参数。
但是,如果我从 URL 中删除用户名(即ssh://host.domain:1234/dirs/repo.git)连接失败,即使我以编程方式设置了用户名(见下文)。
考虑以下用于检查某个存储库是否可访问的程序的 MCVE(除必要外,没有错误检查):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <git2.h>
#include <git2/sys/repository.h>
int my_cred_cb(git_cred **out, const char *url,
const char *username_from_url, unsigned int allowed_types, void *payload)
{
uid_t uid = geteuid(); // Get effective user ID
struct passwd* pw = getpwuid(uid); // Get password file entry
char privkeypath[strlen(pw->pw_dir) + 13];
strcpy(privkeypath, pw->pw_dir);
strcat(privkeypath, "/.ssh/id_rsa"); …Run Code Online (Sandbox Code Playgroud) 根据参考,非类型模板参数的名称是可选的,即使在分配默认值时也是如此(参见(1)和(2))。因此这些模板结构是有效的:
template <int> struct Foo {};
template <unsigned long = 42> struct Bar {};
Run Code Online (Sandbox Code Playgroud)
我还没有看到访问非类型参数值的可能性。我的问题是:未命名/匿名非类型模板参数有什么意义?为什么名称是可选的?
我偶然发现了这段代码来重新建立类不变式:
class Foo {
// some stuff in here
public:
void clear() {
*this = Foo();
//operator=(Foo()); // commented out in favor of the line above
}
};
Run Code Online (Sandbox Code Playgroud)
operator=是合法的并且可以按预期工作,但是在班级不动的情况下,将创建一个不必要的临时调用。因此,手动分配默认值可能会更有效,如果要扩展该类,则默认值将很麻烦且容易出错。*this = Foo(),如果允许的话,可能会更有效,因为我假设复制省略在这里可以工作(不管类是否可移动)。所以我的问题是:
*this = Foo();合法吗?如果是,请提供标准参考Foo是可移动的。(如果您认为这是骗子,请找我正确的问题,我什么也找不到)
在更大的代码库中,我遇到过这样的代码(参见 Godbolt):
struct Foo {};
struct Base {
Foo* a;
};
struct Bar : public Base {
static Foo* Bar::*mybar[];
};
Foo* Bar::Bar::*mybar[] = {
&Base::a
};
Run Code Online (Sandbox Code Playgroud)
老实说,我很困惑。这看起来像是在初始化一个静态Foo指针数组,Bar其中包含一个非静态成员变量Base。没有对象怎么可能呢?
(免责声明:这是在实际工作的生产代码中发现的 - 希望不依赖于 UB?)
另外,如果我删除像这里这样的限定名称查找有什么问题吗?我想重构代码并使其更具可读性。所有这些Bar::似乎都是多余的,但由于我对代码感觉不太舒服,我宁愿先了解其含义。
I was playing around a bit with Concepts offered in C++20 and came up with a simple example, which, to my surprise, does not produce the expected results (please leave any discussion on the usefulness of my example be :-)):
#include <iostream>
#include <type_traits>
#include <vector>
template <typename T>
concept i_am_integral = requires { std::is_integral_v<T> == true; };
template <typename T> requires i_am_integral<T>
using intvector = std::vector<T>;
int main() {
intvector<int> v = { 1, 2, 3 }; // <- …Run Code Online (Sandbox Code Playgroud) 在Debian上,我需要读取一个环境变量,否则返回一个空字符串.这是我的代码:
std::string getEnv(std::string var)
{
char* val = ::getenv(var.c_str());
std::string retVal(val);
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
找不到环境变量时,我收到以下错误:
basic_string::_S_construct null not valid error
Run Code Online (Sandbox Code Playgroud)
调试后,std::string retVal(val)当我尝试向其加载空val时,会发生错误代码.
我从一个空的char *工作中加载了一个字符串,但是看不到.
如何修复上面的代码,以便在环境变量不存在的情况下使其工作?
c++ ×6
c ×2
c++-concepts ×1
c++11 ×1
c++17 ×1
c++20 ×1
clang++ ×1
class ×1
constructor ×1
git ×1
inheritance ×1
libgit2 ×1
libssh2 ×1
non-type ×1
openmp ×1
ssh ×1
static ×1
templates ×1
this-pointer ×1