试图允许make_unique使用私有 ctor 的类时,我发现两种情况之间存在以下奇怪的区别:
class A {
int _i;
A(): _i(7) {}
public:
template<typename... T>
static std::unique_ptr<A> create(T&&... t) {
struct enablePrivateCtor : public A {
using A::A;
};
return std::make_unique<enablePrivateCtor>(std::forward<T>(t)...);
}
void doIt() const {
std::cout << _i << std::endl;
}
};
int main() {
auto a = A::create();
a->doIt();
}
Run Code Online (Sandbox Code Playgroud)
输出:
7
Run Code Online (Sandbox Code Playgroud)
class A {
int _i;
A(int i): _i(i) {} // <- …Run Code Online (Sandbox Code Playgroud) 以下代码在使用 clang(x86_64-pc-linux-gnu 上的 5.0.0-3~16.04.1 版)时崩溃,但与 gcc (9.2.0) 一起工作正常。
struct Registry {
static int registerType(int type) {
std::cout << "registering: " << type;
return type;
}
};
template<typename T>
struct A {
static int i;
};
template<typename T>
int A<T>::i = Registry::registerType(9);
int main() {
std::cout << A<int>::i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
根据地址消毒剂,叮当声崩溃是由于:
ASAN:DEADLYSIGNAL
=================================================================
==31334==ERROR: AddressSanitizer: SEGV on unknown address 0xffffffffffffffe8 (pc 0x7f5cc12b0bb6 bp 0x7ffdca3d1a20 sp 0x7ffdca3d19e0 T0)
==31334==The signal is caused by a READ memory access.
#0 0x7f5cc12b0bb5 in std::ostream::sentry::sentry(std::ostream&) …Run Code Online (Sandbox Code Playgroud) 要打印任何类型,std::pair我们可以实现以下方法:
template<typename First, typename Second>
void printPair(const std::pair<First, Second>& p) {
std::cout << p.first << ", " << p.second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是假设我们要实现一个方法,可以打印任何类型的对,不一定是std::pair,基于以下要求:
first和secondpublic 字段first_type和second_typepublic 内部类型first== 的类型first_typesecond== 的类型second_type有一个concept,我们称之为Pair,可以允许编写一个方法,如:
void printPair(const Pair auto& p) {
std::cout << p.first << ", " << p.second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这样的一个怎么concept定义?
同时努力实现if constexpr与requires clause基于如果constexpr和要求,表达了即席概念检查面临着以下问题:
template<class P>
concept TuplePair = requires(P p) {
requires std::tuple_size<P>::value == 2;
std::get<0>(p);
std::get<1>(p);
};
void print(const auto& p) {
if constexpr( TuplePair<decltype(p)> ) {
std::cout << std::get<0>(p) << ", " << std::get<1>(p) << std::endl;
}
else {
std::cout << "else" << std::endl;
}
}
int main() {
// justifiably prints 'else':
print(std::make_tuple(3, 4, 5));
// prints 'else' even though this is a valid TuplePair:
print(std::make_tuple(1, 2));
}
Run Code Online (Sandbox Code Playgroud)
有什么问题if constexpr …
试图向 SFINAE 说再见。
是否可以使用concepts来区分函数,以便编译器可以根据发送的参数是否满足concept约束来匹配正确的函数?
例如,重载这两个:
// (a)
void doSomething(auto t) { /* */ }
// (b)
void doSomething(ConceptA auto t) { /* */ }
Run Code Online (Sandbox Code Playgroud)
因此,当被调用时,编译器会在每次调用时匹配正确的函数:
doSomething(param_doesnt_adhere_to_ConceptA); // calls (a)
doSomething(param_adheres_to_ConceptA); // calls (b)
Run Code Online (Sandbox Code Playgroud)
约束偏序的规则是指AND和OR,但不指NOT:
Run Code Online (Sandbox Code Playgroud)13.5.4 Partial ordering by constraints [temp.constr.order] (1.2) ... - The constraint A ? B subsumes A, but A does not subsume A ? B. - The constraint A subsumes A ? B, but A ? B does not subsume A.
Run Code Online (Sandbox Code Playgroud)13.5.3 Constraint normalization [temp.constr.normal] 1 The normal form of an expression E is a constraint that is defined as follows: (1.1) The normal form of an expression ( E ) is the normal …
以下代码编译时没有任何错误,但它似乎破坏了 ODR:
#include <iostream>
template<long Num>
class B;
template<long Num>
struct A {
template<long Num1>
friend void ffoo(A<Num1> a, B<Num>* = nullptr) {
std::cout << "@A ffoo(A<" << Num1 << ">, B<" << Num << ">*)" << std::endl;
}
};
template<long Num>
class B {
public:
friend void ffoo(A<Num> a, B<Num>* = nullptr) {
std::cout << "@B ffoo(A<" << Num << ">, B<" << Num << ">*)" << std::endl;
}
};
int main() {
ffoo(A<1>{}); // @A ffoo(A<1>, …Run Code Online (Sandbox Code Playgroud) c++ templates one-definition-rule friend-function language-lawyer
尝试实现以下代码:
template <typename R, typename V>
concept SizedRangeOf =
std::ranges::sized_range<R> &&
std::same_as<std::ranges::range_value_t<R>, V>;
template<typename T>
const SizedRangeOf<T> auto getView(std::vector<T>& vec) {
// helper class
class vector_view {
std::vector<T>& vec;
public:
vector_view(std::vector<T>& vec): vec(vec) {}
auto begin() const { return vec.begin(); }
auto end() const { return vec.end(); }
std::size_t size() const { return vec.size(); }
};
return vector_view { vec };
}
int main() {
std::vector<int> v = {1, 3, 5};
auto r = getView(v);
v.push_back(7);
for(auto val: …Run Code Online (Sandbox Code Playgroud) 我正在使用默认的 Firebase 函数生成电子邮件验证。默认的电子邮件验证成功页面如下所示:

我想自定义电子邮件验证成功后的响应页面。有没有办法做到这一点?
c++ ×9
c++20 ×6
c++-concepts ×5
c++11 ×1
constinit ×1
constructor ×1
firebase ×1
if-constexpr ×1
ostream ×1
sfinae ×1
static ×1
templates ×1
type-traits ×1