考虑以下片段:
#include <cstdint>
#include <iostream>
struct Foo {
Foo() : foo_(0U), bar_(0U) {}
void increaseFoo() { increaseCounter<&Foo::foo_>(); }
void increaseBar() { increaseCounter<&Foo::bar_>(); }
template <uint8_t Foo::*counter>
void increaseCounter() { ++(this->*counter); }
uint8_t foo_;
uint8_t bar_;
};
void callMeWhenever() {
Foo f; // automatic storage duration, no linkage.
f.increaseFoo();
f.increaseFoo();
f.increaseBar();
std::cout << +f.foo_ << " " << +f.bar_; // 2 1
}
int main() {
callMeWhenever();
}
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测会一直说这是病态的,因为f在callMeWhenever()具有自动存储时间,它的地址是不是在编译时已知,而成员模板函数increaseCounter()的Foo与指针的数据成员实例化Foo,以及存储表示给定类类型的特定于编译器(例如填充)。但是,从cppreference / …
(这个问题是从Template specialization of variable template and type deduction的评论中的讨论的一个分支。)
[temp.expl.spec]/10指出 [强调我的]:
尾随 模板参数 可以在模板 id 中未指定, 命名显式函数模板特化,前提是它可以从函数参数类型中推导出来。[?例子:
Run Code Online (Sandbox Code Playgroud)template<class T> class Array { /* ... */ }; template<class T> void sort(Array<T>& v); // explicit specialization for sort(Array<int>&) // with deduced template-argument of type int template<> void sort(Array<int>&);?—?结束示例?]
这显然适用foo(T)于以下示例中的(完全)显式特化:
#include <iostream>
template <typename T>
void foo(T) { std::cout << "primary\n"; }
template <>
void foo(int) { std::cout << "int\n"; …Run Code Online (Sandbox Code Playgroud) 我只是偶然发现了一个override说明符的用例,据我所知,它似乎是多余的,也没有任何特定的语义含义,但也许我错过了一些东西,因此这个问题.在继续之前,我应该指出,我试图在这里找到答案,但最接近我得到的是以下主题,而不是真正回答我的问题(也许有人可以指出一个问答实际上已经回答了我的问题)题).
考虑以下抽象类:
struct Abstract {
virtual ~Abstract() {};
virtual void foo() = 0;
};
Run Code Online (Sandbox Code Playgroud)
在直接派生的非抽象类中override实现时foo(),有没有理由使用说明符Abstract(DerivedB如下所示)?即,当foo()派生类已经需要实现非抽象(并没有真正覆盖任何东西)时?
/* "common" derived class implementation, in my personal experience
(include virtual keyword for semantics) */
struct DerivedA : public Abstract {
virtual void foo() { std::cout << "A foo" << std::endl; }
};
/* is there any reason for having the override specifier here? */
struct DerivedB : …Run Code Online (Sandbox Code Playgroud) 如何从字符串中获取数字字符?我不想转换Int.
var string = "string_1"
var string2 = "string_20_certified"
Run Code Online (Sandbox Code Playgroud)
我的结果必须格式如下:
newString = "1"
newString2 = "20"
Run Code Online (Sandbox Code Playgroud) 这来自Swift标准库文档:
relative(to:)返回此范围表达式描述的给定集合中的索引范围.
这是方法签名:
func relative<C>(to collection: C) -> Range<Self.Bound> where C : _Indexable, Self.Bound == C.Index
Run Code Online (Sandbox Code Playgroud)
除了它的解释:
参数
采集
用于评估此范围表达式的集合.
回报价值
适合切片收集的范围.返回的范围不保证在集合的范围内.呼叫者应该对返回值应用与用户直接提供的范围相同的前提条件.
最后,这是我的测试代码:
let continuousCollection = Array(0..<10)
var range = 0..<5
print(range.relative(to: continuousCollection))
//0..<5
range = 5..<15
print(range.relative(to: continuousCollection))
//5..<15
range = 11..<15
print(range.relative(to: continuousCollection))
//11..<15
let disparateCollection = [1, 4, 6, 7, 10, 12, 13, 16, 18, 19, 22]
range = 0..<5
print(range.relative(to: disparateCollection))
//0..<5
range = 5..<15
print(range.relative(to: disparateCollection))
//5..<15
range = 11..<15
print(range.relative(to: disparateCollection)) …Run Code Online (Sandbox Code Playgroud) 我有一个字符串“00000000000000000000010011010010”。我想在每八个字符后添加一个空格,使其代表一个 32 位整数。例如:“00000000 00000000 00000100 11010010”。什么是最简单的方法来做到这一点?
考虑以下示例:
void f() requires true { }
int main() { f(); }
Run Code Online (Sandbox Code Playgroud)
Clang (1) ( DEMO ) 接受这个程序,而 GCC (1) ( DEMO ) 拒绝它并出现以下错误:
Run Code Online (Sandbox Code Playgroud)error: constraints on a non-templated function
请注意,约束表达式实际上可以在 Clang 的情况下使用,因为以下程序被 Clang 拒绝:
void f() requires false { }
int main() { f(); } // // error: no matching function for call to 'f'
Run Code Online (Sandbox Code Playgroud)
Clang 注意到声明f的不是候选者,因为约束不满足(因为它是f()调用的候选者;DEMO)。
(1) GCC HEAD 11.0.0 20210124 和 Clang HEAD 12.0.0 (20210124), -std=c++20.
对于下面的代码,编译器是否有任何原因会选择非显式构造函数。
struct S {
S() = default;
explicit S(S & cp) {
std::cout << "explicit" << std::endl;
}
S(const S & cp) {
std::cout << "copy" << std::endl;
};
};
int main() {
S s1;
S s2 = {s1};
}
Run Code Online (Sandbox Code Playgroud)
来自https://en.cppreference.com/w/cpp/language/list_initialization,它说对于copy-list-ini...显式和非显式构造函数都被考虑,但只能调用非显式构造函数
我是ios的新手,用swift开发我的第一个应用程序.此应用程序包含许多弹出窗口与应用程序播放.我想让这个非常有吸引力.
内置的UIAlertcontroller破坏了我的外观和感觉.那么有没有办法完全自定义UIAlertController.
我想改变字体和颜色的title,消息还背景颜色和按钮太多.
谢谢.
什么规则使以下代码编译无错误:
using integer = int;
struct Foo
{
int integer;
};
int main() {
Foo f;
int integer;
f.integer;
}
Run Code Online (Sandbox Code Playgroud)
using当然不是 的简单替代品#define integer int,但是是什么让这段代码看起来格式良好,而int int;会使其格式错误?