小编dfr*_*fri的帖子

使用inout关键字:是通过引用传递的参数还是通过copy-in copy-out(/call by value result)

问题:基于以下信息和讨论:inout参数是通过引用传递还是通过拷入拷出


基于以下SO线程,通过引用传递inout关键字标记的函数参数:

我们注意到最顶层的两个线程是 Swift 2.0 之前的;我在 SO 上找不到关于该主题的任何更新的讨论(除了有点相关的第三个线程链接)。


但是,根据 Apple 的文档(据我所知),由inout关键字标记的函数参数是通过copy-in copy-out(或按值调用结果)传递的

传入传出参数如下:

当函数被调用时,参数的值被复制。在函数体中,副本被修改。当函数返回时,副本的值被分配给原始参数。此行为称为拷入拷出或按值结果调用。...

... 您可以通过将 inout 关键字放在其参数定义的开头来编写输入输出参数。一个输入输出参数有一个传入函数的值,被函数修改,然后传回函数外来替换原来的值。...


现在我自己的例子试图调查这个:

struct MyStruct {
    private var myInt: Int

    mutating func increaseMyInt() {
        myInt++
    }

    func …
Run Code Online (Sandbox Code Playgroud)

swift2 inout

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

循环中Swift样式的多个增量

我的Swift代码中有以下循环:

for var i:Int = 0, j:Int = 0; i < rawDataOut.count; i += 4, ++j {
    maskPixels[j] = rawDataOut[i + 3]
}
Run Code Online (Sandbox Code Playgroud)

我在Xcode中收到两个警告:

  1. 不推荐使用C-style for语句,将在未来的Swift版本中删除
  2. '++'已被弃用:它将在Swift 3中删除

我无法看到如何在Swift的For In Loops中重写这个,以考虑两个变量和4步,而不会让它变得混乱.有简单优雅的翻译吗?

swift swift3

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

将命名空间范围文件 - 本地(.cpp)常量放在匿名命名空间中

在命名空间范围内声明的(文件本地; .cpp)const限定变量具有内部链接,因此是转换单元本地.在匿名命名空间中仍然存在仍然包装常量的原因吗?

例如,有没有理由更喜欢以下两种中的任何一种,如果是这样,为什么呢?

// file.cpp
namespace foo {

const int kMyLocalConstant = 42;  // internal linkage

}  // namespace foo
Run Code Online (Sandbox Code Playgroud)

VS

// file.cpp
namespace foo {
namespace {

const int kMyLocalConstant = 42;  // internal linkage

}  // namespace
}  // namespace foo
Run Code Online (Sandbox Code Playgroud)

我很高兴能得到C++ 03以及C++ 11的答案,如果这个上下文中有两个不同的话.


可能重复

我已经阅读了优秀的答案

但我没有看到它回答我的具体问题(如果我错了请纠正我),因为答案集中在非const变量标识符和非静态自由函数上.我的问题集中在文件本地命名空间范围的常量,即已经具有内部链接的变量标识符.也许有一个我没有找到的更合适的骗局.

c++ namespaces linkage c++11 c++03

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

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

考虑以下片段:

#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
查看次数

使 clang 格式的订单以不区分大小写的方式包含

我正在尝试配置 .clang-format。我的目的是让它保留我的包含组,因为它们在代码中,但按字母顺序对它们进行排序。我设法接近我想要的IncludeBlocks: Preserve,但它以区分大小写的方式包含在每个块中:

这是我应用格式后得到的结果

#include "A1.h"
#include "B2.h"
#include "a2.h"
#include "b1.h"
Run Code Online (Sandbox Code Playgroud)

这就是我想要实现的目标

#include "A1.h"
#include "a2.h"
#include "b1.h"
#include "B2.h"
Run Code Online (Sandbox Code Playgroud)

我正在使用 clang-format 版本 10.0。我的 .clang 格式文件,以防相关:

---
# Brompton's Clang Format file v0.0.1
#
# Changelog:
# v0.0.1 (25/9/2020):
# - First version of this file


# Base it on Google's Standard, with 4 spaces as indentation
BasedOnStyle: Google
IndentWidth: '4'
Standard: Cpp11
UseTab: Never

# Settings for C/C++
Language: Cpp
# 120 chars per line, reflow …
Run Code Online (Sandbox Code Playgroud)

c++ clang-format

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

在类模板的其他未定义成员类的显式特化中定义隐藏朋友是否合法?

根据[temp.expl.spec]合法放置显式专的规则并不完全容易掌握,尤其是在与隐藏的朋友混合时,以避免我的专的自焚[temp.expl.spec]/8和友元函数 我希望对以下内容有第二意见(1)

  • 在类模板的头文件中,在类模板的其他未定义(/仅声明)成员类的显式特化中定义隐藏朋友是否合法?假设该报头包含在至少两个不同的翻译单元中。

(1)根据以下标准参考资料,我自己对标准的解释是“是的;这是合法的”。


或者,举个例子:下面的程序格式正确吗?

// s.h
#pragma once

template <int N>
struct S {
    struct M;
};
      
template<>
struct S<42>::M {
    friend int f(M) { return 42; }
};

// foo.h
#pragma once
void foo();

// foo.cpp
#include "foo.h"
#include <iostream>
#include "s.h"

void foo() {
    std::cout << f(S<42>::M{});
}

// main.cpp
#include <iostream>
#include "foo.h"
#include "s.h"

int main() {
    std::cout << f(S<42>::M{});
    foo();
}
Run Code Online (Sandbox Code Playgroud)

请注意,根据[class.friend]/7 …

c++ templates friend template-specialization language-lawyer

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

具有先前模板参数类型的自动返回类型参数的函数类型的模板参数

我有一个带有两个参数的模板:第一个是类型,第二个是带有参数的函数指针,其类型是第一个模板参数。这个 MCVE 有效:

void returnsVoid(int x) { }

template <typename T, void (*Func)(T)>
struct foo { void bar(T t) { Func(t); } };

int main(int, char *[]) {
    foo<int, returnsVoid> a; // ok
}
Run Code Online (Sandbox Code Playgroud)

但是,当我将第二个模板参数的返回类型更改为auto(如本相关问题中所述)时,出现错误:

void returnsVoid(int x) { }

template <typename T, auto (*Func)(T)>
struct foo {
    void bar(T t) { Func(t); }
};

int main(int, char *[]) {
    foo<int, returnsVoid> a; // error: unable to deduce ‘auto (*)(T)’ from ‘returnsVoid’
                             // note:   mismatched types …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer c++17

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

“用作非类型模板参数”是否使函数模板隐式实例化?

我想编写一个M接受不完整类型C作为模板参数的类模板。但我也希望C在最终定义时具有一些特征。

此代码是否有保证

  • 编译如果定义(标志),
  • 如果 !defined(FLAG) 编译失败?
template <auto> struct Dummy {};

template <typename C>
void check()
{
    static_assert(std::is_trivial_v<C>);
}

template <typename C>
struct M : Dummy<&check<C>>
{
    //static_assert(std::is_trivial_v<C>);//error: incomplete type
    C * p;
};

struct Test;
M<Test> m;

int main()
{
    return 0;
}

#if defined(FLAG)
struct Test {};
#else
struct Test { std::string non_trivial_member; };
#endif
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer function-templates implicit-instantiation non-type-template-parameter

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

什么语言规则规定模板化数组&amp;&amp;函数参数中的“T&amp;&amp;”不是转发引用?

我正在使用的静态分析工具提示我需要std::forward调用链中以下函数的参数:

template<typename T, std::size_t size>
constexpr void f(T (&& arr)[size]) { /* linter: use std::forward on 'arr' here */ }
Run Code Online (Sandbox Code Playgroud)

因为它将函数参数类型标识为转发引用。

然而,对多个编译器的简单测试表明,这不是转发引用。下面的例子

void g() {
    int a[3]{1, 2, 3};
    f(a);  // #1
}
Run Code Online (Sandbox Code Playgroud)

#1在(DEMO )处被拒绝,明确指出函数参数是右值引用:

 error: cannot bind rvalue reference of type 'int (&&)[3]' to lvalue of type 'int [3]'
Run Code Online (Sandbox Code Playgroud)

问题

  • 哪些语言规则控制着上面T&&的模板化数组函数&&参数f不是转发引用?

c++ templates language-lawyer forwarding-reference

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