小编dfr*_*fri的帖子

指向(数据)成员作为非类型模板参数的指针,例如具有自动存储持续时间/无链接

考虑以下片段:

#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)

我的第一个猜测会一直说这是病态的,因为fcallMeWhenever()具有自动存储时间,它的地址是不是在编译时已知,而成员模板函数increaseCounter()Foo与指针的数据成员实例化Foo,以及存储表示给定类类型的特定于编译器(例如填充)。但是,从cppreference / …

c++ templates pointer-to-member

5
推荐指数
1
解决办法
106
查看次数

在函数模板的显式特化声明中推导尾随模板参数(无函数参数推导)

(这个问题是从Template specialization of variable template and type deduction的评论中的讨论的一个分支。)


[temp.expl.spec]/10指出 [强调我的]:

尾随 模板参数 可以模板 id 中未指定, 命名显式函数模板特化,前提是它可以从函数参数类型中推导出来。[?例子:

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>&);
Run Code Online (Sandbox Code Playgroud)

?—?结束示例?]

这显然适用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)

c++ templates template-specialization language-lawyer

5
推荐指数
1
解决办法
58
查看次数

从抽象基类实现纯虚函数:重写说明符有什么意义吗?

背景

我只是偶然发现了一个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)

c++

4
推荐指数
1
解决办法
1175
查看次数

从字符串中获取数字字符

如何从字符串中获取数字字符?我不想转换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 swift3

4
推荐指数
2
解决办法
6993
查看次数

相对(to :)函数究竟做了什么?

这来自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)

collections range swift swift4

3
推荐指数
1
解决办法
392
查看次数

在特定位置为字符串添加空格

我有一个字符串“00000000000000000000010011010010”。我想在每八个字符后添加一个空格,使其代表一个 32 位整数。例如:“00000000 00000000 00000100 11010010”。什么是最简单的方法来做到这一点?

c++ string binary whitespace range-v3

3
推荐指数
1
解决办法
97
查看次数

非模板化函数的尾随要求子句的编译器差异

考虑以下示例:

void f() requires true { }
int main() { f(); }
Run Code Online (Sandbox Code Playgroud)

Clang (1) ( DEMO ) 接受这个程序,而 GCC (1) ( DEMO ) 拒绝它并出现以下错误:

error: constraints on a non-templated function
Run Code Online (Sandbox Code Playgroud)

请注意,约束表达式实际上可以在 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.

c++ gcc clang language-lawyer c++20

3
推荐指数
1
解决办法
91
查看次数

为什么编译器总是选择非显式构造函数来进行复制列表初始化?

对于下面的代码,编译器是否有任何原因会选择非显式构造函数。

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...显式和非显式构造函数都被考虑,但只能调用非显式构造函数

https://godbolt.org/z/YqWWs9jqY

c++ copy-constructor language-lawyer c++11

3
推荐指数
1
解决办法
126
查看次数

ios - 我想根据我的应用主题完全自定义uialertcontroller

我是ios的新手,用swift开发我的第一个应用程序.此应用程序包含许多弹出窗口与应用程序播放.我想让这个非常有吸引力.

内置的UIAlertcontroller破坏了我的外观和感觉.那么有没有办法完全自定义UIAlertController.

我想改变字体和颜色title,消息背景颜色和按钮太多.

谢谢.

ios swift uialertcontroller swift2

2
推荐指数
1
解决办法
5497
查看次数

为什么允许类型别名作为变量名?

什么规则使以下代码编译无错误

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;会使其格式错误?

c++ using language-lawyer type-alias

2
推荐指数
1
解决办法
160
查看次数