这是真的,即结构化绑定在clang(我用最近建造clang version 4.0.0 (trunk 282683))用一些东西来实现<tuple>,如括号,初始化列表可以使用的东西从<initializer_list>?
我编写了简单的代码只是为了使用一些最新的功能:
struct S { int a; char b; double c; };
auto [a, b, c] = S{1, '2', 3.0};
using A = decltype(a);
using A = int;
using B = decltype(b);
using B = char;
using C = decltype(c);
using C = double;
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,但是当我const之前添加限定符时auto:
struct S { int a; char b; double c; };
const auto [a, b, c] …Run Code Online (Sandbox Code Playgroud) 扔烂番茄之前
我知道lambda分解的实际应用目前受到限制,因为无法找到替代失败友好的方法来检查隐藏在分解变量中的lambda捕获数量。这只是一个理论问题,因为我找不到涵盖捕获成员变量访问修饰符的任何标准部分。
例
int main() {
int a;
auto [x] = [a]{};
static_cast<void>(a);
static_cast<void>(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
标准参考
关于lambda捕获的标准部分很长,因此我可能错过了相关的片段。我注意到的是,重点是对应于捕获的非静态成员必须/未命名。
假设我有这样一个元组。
std::tuple<int &, int> tuple{};
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
auto [i1, i2] = tuple; // Here i1 is lvalue reference, i2 is int
Run Code Online (Sandbox Code Playgroud)
i1 是左值引用,因为元组上的第一个值是一个左值引用。不过,我没有写auto &[i1, i2]。那么,在这种情况下是否有可能“删除”引用?所以我得到了 i1 和 i2 作为“简单”int。谢谢 !
为什么通过唯一命名的变量定义结构化绑定,而所有模糊的“名称都绑定到”语言呢?
我个人认为结构化绑定的工作方式如下。给定一个结构:
struct Bla
{
int i;
short& s;
double* d;
} bla;
Run Code Online (Sandbox Code Playgroud)
下列:
cv-auto ref-operator [a, b, c] = bla;
Run Code Online (Sandbox Code Playgroud)
(大致)等于
cv-auto ref-operator a = bla.i;
cv-auto ref-operator b = bla.s;
cv-auto ref-operator c = bla.d;
Run Code Online (Sandbox Code Playgroud)
以及数组和元组的等效扩展。但是显然,这太简单了,并且所有这些模糊的特殊语言都用来描述需要发生的事情。
因此,我显然遗漏了一些东西,但是在某种意义上讲,如果可以说是一个折叠定义(在标准语言中更容易阅读)的定义明确的扩展,那究竟是什么情况呢?
似乎由结构化绑定定义的变量的所有其他行为实际上都遵循我认为将用于定义概念的简单扩展“规则”。
我很想 通过以下方式解决这个问题:
#include <iostream>
#include <set>
#include <iterator>
#include <array>
#include <tuple>
#include <type_traits>
int main()
{
const std::set<int> s{ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
auto iter = s.find(5);
using IterType = decltype(iter);
// using `std::array` works fine!
const auto& [pv1, nxt1] = std::array<IterType, 2>{std::prev(iter), std::next(iter)};
std::cout <<"using std::array<IterType, 2> :"<< *pv1 << " " << *nxt1 << '\n'; // prints: 4 6
// using ` std::make_tuple` works fine!
const auto& [pv2, nxt2] …Run Code Online (Sandbox Code Playgroud) 有没有办法在解构 a 时隐藏现有变量std::pair?例如,如果我定义了以下函数:
#include <iostream>
#include <utility>
std::pair<int, int> returns_pair(int a, int b)
{
return std::make_pair(a * a, b * b);
}
void prints_two_variables(int a, int b)
{
std::cout << a << "\n" << b << "\n";
}
Run Code Online (Sandbox Code Playgroud)
然后这个main函数工作正常,因为我从返回的中创建了新变量std::pair:
int main()
{
int a = 2;
int b = 3;
auto [x, y] = returns_pair(a, b);
prints_two_variables(x, y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)4 9
但是我不能使用相同的变量名并隐藏现有变量,因为这会尝试再次实际声明它们:
int main()
{
int a = 2;
int b …Run Code Online (Sandbox Code Playgroud) 我认为使用结构化绑定和auto&说明符可以获得对结构成员的引用并直接使用它们而不是通过结构。
但是,以下代码有效并且静态断言成立:
struct Test
{
int i;
char c;
double d;
};
Test test{ 0, 1, 2 };
auto& [i, c, d] = test;
i = 4;
c = 5;
d = 6;
// i, c, d are not references !
static_assert(!std::is_same_v<decltype(i), int&>);
static_assert(!std::is_same_v<decltype(c), char&>);
static_assert(!std::is_same_v<decltype(d), double&>);
cout << &i << " == " << &test.i << " (" << std::boolalpha << (&i == &test.i) << ")" << endl; // (true)
cout << test.i << ", " << …Run Code Online (Sandbox Code Playgroud) 我刚刚了解了 C++ 中的结构化绑定,但有一件事我不喜欢
auto [x, y] = some_func();
Run Code Online (Sandbox Code Playgroud)
就是隐藏了和auto的类型。我必须查找声明才能知道and的类型。或者,我可以写xysome_funcxy
T1 x;
T2 y;
std::tie(x, y) = some_func();
Run Code Online (Sandbox Code Playgroud)
但这仅在 和x是y默认可构造且不是 的情况下才有效const。有没有办法写
const auto [x, y] = some_func();
Run Code Online (Sandbox Code Playgroud)
x对于和 的非默认构造类型,以使得和 的y类型可见的方式?当我将and声明为与的返回类型不兼容(即not )时,编译器最好应该抱怨。xyxysome_funcconst auto /* T1, T2 */ [x, y] = some_func();
澄清。由于我的问题下面的评论似乎围绕是否使用&,并且以前的一些答案将我的问题误解为“使用哪种语法来提取返回对的数据类型”,我认为我需要澄清我的问题。
假设我们的代码分布在多个文件中
//
// API.cpp
//
#include <utility>
class Foo {
public:
Foo () {}
}; …Run Code Online (Sandbox Code Playgroud) 考虑到可以创建这样的匿名结构:
#include <iostream>
struct {
int a;
int b;
} my_anonymous_struct = { 2,3 };
int main() {
std::cout << my_anonymous_struct.a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,是什么原因导致错误出现呢?
#include <iostream>
struct file {
int min;
int max;
};
auto read_historic_file_dates(file F) -> struct { int min; int max; } {
return { F.min, F.max };
}
int main() {
file F = { 1, 4 };
auto [min_date, max_date] = read_historic_file_dates(F);
}
Run Code Online (Sandbox Code Playgroud)
错误:
<source>:8:42: error: declaration of anonymous struct must be a definition …Run Code Online (Sandbox Code Playgroud) 序言:
\n我知道有相当多的主题具有相同或相似的标题。继续阅读,你就会明白我的情况。
我是 C++ 新手,目前正在读一本很明显有损坏示例练习的书。
\n我的代码
#include <iostream>\n\nusing namespace std;\n\nstruct Point {\n int x;\n int y;\n};\n\nint main(int argc, char const* argv[]) {\n\n Point p1{100, 200};\n\n auto [a, b] = p1;\n\n cout << "a = " << a << endl;\n cout << "b = " << b << endl;\n\n auto& [c, d] = p1;\n\n c += 50;\n d += 50;\n\n cout << "p1.x = " << p1.x << endl;\n cout << "p1.y = " << p1.y << endl;\n\n …Run Code Online (Sandbox Code Playgroud)