这段代码:
#include <iostream>
#include <vector>
using namespace std;
void dump(const std::string& s) {
cout << s << endl;
}
class T {
public:
T() {
dump("default ctor");
}
T(std::nullptr_t) {
dump("ctor from nullptr_t");
}
T(const T&) {
dump("copy ctor");
}
T& operator=(const T&) {
dump("copy operator=");
return *this;
}
T& operator=(std::nullptr_t) {
dump("operator=(std::nullptr_t)");
return *this;
}
T& operator=(const std::vector<int>&) {
dump("operator=(vector)");
return *this;
}
};
int main() {
T t0;
t0 = {};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产出:
default ctor …Run Code Online (Sandbox Code Playgroud) 关于这个问题的讨论是在这个非常简单的问题的答案下开始的。
\n这个简单的代码具有意外的构造函数重载解析std::basic_string:
#include <string>\n\nint main() {\n std::string s{"some string to test", 2, 3};\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n现在有人期望这会调用这个构造函数:
\nstd::basic_string<CharT,Traits,Allocator>::basic_string - cppreference.com
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
template<class T> basic_string( const T& t, size_type pos, size_type n, const Allocator& alloc = Allocator() ); (自 C++17 起) (11) template< class T > constexpr basic_string( const T& t, size_type pos, size_type const Allocator& alloc = Allocator() ); (自 C++20 起) (11)
基本原理基于此 C++ …
decltype(auto) func(int&& n)
{
return (n);
}
Run Code Online (Sandbox Code Playgroud)
clang 12.0.1接受此代码,但gcc 11.2.1拒绝它:
error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'int'
Run Code Online (Sandbox Code Playgroud)
gcc似乎使用 return type int &&,但带有n内括号,这是正确的吗?
我在p12中有一个私钥,其密码设置为空.现在,当我尝试SecPKCS12Import在OS X或iOS上使用导入此私钥时,我遇到了错误(相当于Windows工作).
- (NSError *)setClientIdentityCertificateFromPKCS12Data: (NSData *)PKCS12Data withPassword: (NSString *)password
{
OSStatus securityError = errSecSuccess;
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { (__bridge CFStringRef)password };
CFDictionaryRef optionsDictionary = NULL;
optionsDictionary = CFDictionaryCreate(
NULL, keys,
values, (password?1:0),
NULL, NULL);
CFArrayRef items = NULL;
securityError = SecPKCS12Import((__bridge CFDataRef)PKCS12Data,
optionsDictionary,
&items);
Run Code Online (Sandbox Code Playgroud)
密码为空时我尝试了不同的组合:
optionsDictionary = NULLoptionsDictionary 没有价值观optionsDictionary与@""关键值kSecImportExportPassphrase它始终securityError不等于errSecSuccess.分别:
securityError=-25260 "导入/导出需要密码短语."securityError=-25260 "导入/导出需要密码短语."securityError=-25264 "PKCS12导入期间MAC验证失败(密码错误?)"现在我很好,它不起作用.我认为p12没有密码是一种安全威胁,但如果这就是它无法工作的原因,我需要一些文件来说明这一点.到目前为止,我已经试图谷歌没有运气了. …
我需要为我的项目使用 C++ 17 的文件系统头文件。据我所知,Apple 终于在 Xcode 11 和 macOS Catalina 中提供了它。我使用的是最新的(beta 3)Xcode 11,我使用的是 macOS Catalina public beta 2,所以理论上它应该可以工作。但由于某种原因,它不是,Xcode 给出了如下错误:
'~path' is unavailable: introduced in macOS 10.15
Run Code Online (Sandbox Code Playgroud)
如果我将 Build Setting 中的 C++ 标准库从 libc++ 设置为 libstdc++,这些错误消息就会消失,我收到警告:
include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
Run Code Online (Sandbox Code Playgroud)
以及在各种文件中缺少 iostream 和 cstddef 的大量错误。我该怎么做才能使文件系统工作?
编辑:一个最小的代码示例
#include <filesystem>
#include <iostream>
#include <string>
bool isPathDir(std::string pathString);
int main(int argc, char *argv[])
{
std::string pathString = "../test.jpg"; …Run Code Online (Sandbox Code Playgroud) 我写了一些服务,使用BroadcastReceiver来捕获媒体按钮之一(来自耳机的"播放按钮"),它在Android 2.3.x(HTC Nexus One或HTC Desire)上运行完美
当我试图在Android 4.0.3(三星Nexus S)上运行它不起作用(我的应用程序没有收到意图"android.intent.action.MEDIA_BUTTON"和"播放"按钮表现如常:停止/启动音乐).
清单内容:
Run Code Online (Sandbox Code Playgroud)... <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".buttonreceiver.MediaButtonIntentReceiver" > <intent-filter android:priority="10000" > <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> ...
有没有办法让它在android 4.0.3上运行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_and_activation_view);
Log.d("MR", "onCreate - " + getIntent().getAction());
mReceiver = new MediaButtonIntentReceiver();
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_MEDIA_BUTTON));
}
Run Code Online (Sandbox Code Playgroud)
现在我完全糊涂了.
android broadcastreceiver android-manifest android-4.0-ice-cream-sandwich
考虑以下代码:
#include <vector>
#define BROKEN
class Var {
public:
#ifdef BROKEN
template <typename T>
Var(T x) : value(x) {}
#else
Var(int x) : value(x) {}
#endif
int value;
};
class Class {
public:
Class(std::vector<Var> arg) : v{arg} {}
std::vector<Var> v;
};
Run Code Online (Sandbox Code Playgroud)
不管是否BROKEN定义,Clang ++(7.0.1)都会编译此错误,而如果BROKEN已定义,则g ++(8.2.1)会引发错误:
main.cpp:9:20: error: cannot convert ‘std::vector<Var>’ to ‘int’ in initialization
Var(T x) : value(x) {}
^
Run Code Online (Sandbox Code Playgroud)
据我所知,std::vector(std::vector&&)在两种情况下,这里使用的统一初始化都应选择构造函数。但是,显然,g++它将{arg}视为初始化列表,并尝试初始化应用于向量的vwith 的第一个元素Var,这将不起作用。
如果BROKEN未定义,则g ++显然足够聪明,可以意识到initializer_list重载将无法正常工作。
哪个是正确的行为,或者标准都允许? …
尝试编译以下代码:
#include <functional>
void test() {
int a = 5;
std::function<void()> f = [a](){
[a]()mutable{ // isn't it capture 'a' by copy???
a = 13; // error: assignment of read-only variable 'a'
}();
};
}
Run Code Online (Sandbox Code Playgroud)
给出error: assignment of read-only variable 'a'错误。
通过在a捕获中添加花括号来更改代码:
#include <functional>
void test() {
int a = 5;
std::function<void()> f = [a](){
[a{a}]()mutable{ // this explicitly copies a
a = 13; // error: assignment of read-only variable ‘a’
}();
};
}
Run Code Online (Sandbox Code Playgroud)
消除编译错误。我想知道为什么会这样?第一个变体不等同于第二个变体吗?
这是使用 …
重载解析倾向于将 {} 视为某种基本类型,而不是某种容器。
例如:
#include <iostream>
#include <string>
void foo(const std::string&) {std::cout << "string\n";}
void foo(int) {std::cout << "int\n";}
int main() { foo({}); }
Run Code Online (Sandbox Code Playgroud)
编译时无需任何诊断和输出:
整数
https://godbolt.org/z/zETfrs5as
如果注释掉重载int,那么它可以很好地与string.
问题是为什么?从程序员的角度来看,这可能是令人困惑的错觉。
我正在尝试扩展SocketRocket库的功能.我想添加身份验证功能.
由于此库使用CFNetwork CFHTTPMessage*API进行HTTP功能(需要启动Web套接字连接),我正在尝试使用此API来提供身份验证.
有完美匹配的功能:CFHTTPMessageAddAuthentication,但它不能像我期望的那样工作(据我理解的文档).
以下是显示问题的代码示例:
- (CFHTTPMessageRef)createAuthenticationHandShakeRequest: (CFHTTPMessageRef)chalengeMessage {
CFHTTPMessageRef request = [self createHandshakeRequest];
BOOL result = CFHTTPMessageAddAuthentication(request,
chalengeMessage,
(__bridge CFStringRef)self.credentials.user,
(__bridge CFStringRef)self.credentials.password,
kCFHTTPAuthenticationSchemeDigest, /* I've also tried NULL for use strongest supplied authentication */
NO);
if (!result) {
NSString *chalengeDescription = [[NSString alloc] initWithData: CFBridgingRelease(CFHTTPMessageCopySerializedMessage(chalengeMessage))
encoding: NSUTF8StringEncoding];
NSString *requestDescription = [[NSString alloc] initWithData: CFBridgingRelease(CFHTTPMessageCopySerializedMessage(request))
encoding: NSUTF8StringEncoding];
SRFastLog(@"Failed to add authentication data `%@` to a request:\n%@After a chalenge:\n%@",
self.credentials, requestDescription, chalengeDescription); …Run Code Online (Sandbox Code Playgroud)