问题:基于以下信息和讨论: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) 我的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中收到两个警告:
我无法看到如何在Swift的For In Loops中重写这个,以考虑两个变量和4步,而不会让它变得混乱.有简单优雅的翻译吗?
在命名空间范围内声明的(文件本地; .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变量标识符和非静态自由函数上.我的问题集中在文件本地命名空间范围的常量,即已经具有内部链接的变量标识符.也许有一个我没有找到的更合适的骗局.
考虑以下片段:
#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) 我正在尝试配置 .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) 根据[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
我有一个带有两个参数的模板:第一个是类型,第二个是带有参数的函数指针,其类型是第一个模板参数。这个 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) 我想编写一个M
接受不完整类型C
作为模板参数的类模板。但我也希望C
在最终定义时具有一些特征。
此代码是否有保证
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
我正在使用的静态分析工具提示我需要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 )处被拒绝,明确指出函数参数是右值引用:
Run Code Online (Sandbox Code Playgroud)error: cannot bind rvalue reference of type 'int (&&)[3]' to lvalue of type 'int [3]'
T&&
的模板化数组函数&&
参数f
不是转发引用?c++ ×8
templates ×5
c++03 ×1
c++11 ×1
c++17 ×1
clang-format ×1
friend ×1
inout ×1
linkage ×1
namespaces ×1
non-type-template-parameter ×1
swift ×1
swift2 ×1
swift3 ×1