auto

Using auto in loops c++

让人想犯罪 __ 提交于 2019-11-28 09:36:25
I get warning signed/unsigned mismatch for the following code: auto n = a.size(); for (auto i = 0; i < n; i++) { } The problem is that by assigning 0 to i it becomes int rather than size_t . So what is better: size_t n = a.size(); for (size_t i = 0; i < n; i++) { } or this: auto n = a.size(); for (size_t i = 0; i < n; i++) { } or maybe you have a better solution? I like the first one more because it is bit more consistent, it just uses size_t rather than both size_t and auto for the same purpose. A range based loop could be a cleaner solution: for (const auto& i : a) { } Here, i is a const

Is auto an optional keyword in ranged based for loops?

泄露秘密 提交于 2019-11-28 07:23:54
问题 I recall someone once telling me, "there is no need for auto inside range-based for loops. It would not be ambiguous in the language if we were to remove it." Is that a true statement? Is the folowing code valid C++ syntax? for (elem : range){...} I had assumed this was already valid syntax, but when I went to compile with clang++ --std=c++1z , I was shown the following error: range-based for loop requires type for loop variable for (elem: range){ The compiler still recognizes this as a range

Elasticsearch Query DSL (二)—— Match

Deadly 提交于 2019-11-28 07:15:00
目录 引言 构建示例 match operator 参数 analyzer lenient 参数 Fuzziness fuzzniess 参数 什么是模糊搜索? Levenshtein Edit Distance fuzziness 参数取值规则 prefix_length Zero terms Query Cutoff frequency synonyms 小结 参考文档 系列文章列表 Query DSL Java Rest Client API 引言 昨天是感恩节,上幼儿园的女儿在老师的叮嘱下,晚上为我和老婆洗了脚(形式上的^_^),还给我们每人端了一杯水。看着孩子一天天的长大,懂事,感觉很开心,话说咱们程序员这么辛苦是为了什么?不就是为了老婆,孩子,热炕头,有一个温暖幸福的家庭,再捎带着用代码改变一下世界吗?想到这里,顿时觉得学习,创作博客的劲头也的更足了。哈哈,扯远了,书归正传,今天我们来聊聊 Match Query。 Match Query 是最常用的 Full Text Query 。无论需要查询什么字段, match 查询都应该会是首选的查询方式。它既能处理全文字段,又能处理精确字段。 构建示例 为了能够在后面能深入理解 Match Query 中的各个属性的意义,我们先构建一个 index 示例(有兴趣的同学只要将下面字段粘贴到 sense 中就可以创建)。 PUT

Is it well-formed, if I redefine a variable as auto, and the deduced type is the same? [duplicate]

前提是你 提交于 2019-11-28 03:18:01
问题 This question already has answers here : Does a declaration using “auto” match an extern declaration that uses a concrete type specifier? (3 answers) Closed last year . Look at this snippet: int a; extern int b; auto b = a; Is it well-formed? Clang successfully compiles it, but GCC and MSVC don't. (This issue has come up when I answered How to declare and define a static member with deduced type?) 回答1: Tl;DR; clang is correct, the logic is that this is allowed by [dcl.spec.auto] and to

Observing weird behavior with 'auto' and std::minmax

自古美人都是妖i 提交于 2019-11-28 03:11:30
问题 I am using GCC 4.7.2 and Boost 1.58.0 on SUSE Enterprise Linux 11. I have the following code snippet which basically goes through a list of polygons to compute their length/width. I'm seeing strange output when using the 'auto' keyword with the std::minmax function. To compare, I also declare a second variable where the types are explicitly declared (i.e., dim vs dim1). namespace gtl = boost::polygon; typedef gtl::polygon_90_data<int> LayoutPolygon; typedef gtl::rectangle_data<int>

unexpected copies with foreach over a map

纵然是瞬间 提交于 2019-11-28 02:39:00
问题 I am trying to loop over the entries of a map, and I get unexpected copies. Here is the program: #include <iostream> #include <map> #include <string> struct X { X() { std::cout << "default constructor\n"; } X(const X&) { std::cout << "copy constructor\n"; } }; int main() { std::map<int, X> numbers = {{1, X()}, {2, X()}, {3, X()}}; std::cout << "STARTING LOOP\n"; for (const std::pair<int, X>& p : numbers) { } std::cout << "ENDING LOOP\n"; } And here is the output: default constructor copy

auto from const std::vector<>&; object or reference?

孤街醉人 提交于 2019-11-28 02:30:23
问题 suppose we have an object with the following interface: struct Node_t { ... const std::vector< something >& getChilds() const; } node; Now, i access the property with an auto variable like this: auto childs = node->getChilds(); what is the type of childs ? a std::vector< something > or a reference to one? 回答1: The type of childs will be std::vector<something> . auto is powered by the same rules as template type deduction. The type picked here is the same that would get picked for template

Purpose of perfect forwarding for Callable argument in invocation expression?

孤人 提交于 2019-11-28 01:51:43
In Scott Meyer's book Effective Modern C++ on page 167 (of the print version), he gives the following example: auto timeFuncInvocation = [](auto&& func, auto&&... params) { // start timer; std::forward<decltype(func)>(func)( std::forward<decltype(params)>(params)... ); // stop timer and record elapsed time; }; I completely understand the perfect forwarding of params , but it is unclear to me when perfect forwarding of func would ever be relevant. In other words, what are the advantages of the above over the following: auto timeFuncInvocation = [](auto&& func, auto&&... params) { // start timer

How do I declare a function whose return type is deduced?

守給你的承諾、 提交于 2019-11-27 23:30:40
问题 Consider this C++1y code (LIVE EXAMPLE): #include <iostream> auto foo(); int main() { std::cout << foo(); // ERROR! } auto foo() { return 1234; } The compiler (GCC 4.8.1) generously shoots out this error: main.cpp: In function ‘int main()’: main.cpp:8:18: error: use of ‘auto foo()’ before deduction of ‘auto’ std::cout << foo(); ^ How do I forward-declare foo() here? Or maybe more appropriately, is it possible to forward-declare foo() ? I've also tried compiling code where I tried to declare

Forcing auto to be a reference type in a range for loop

这一生的挚爱 提交于 2019-11-27 23:22:39
问题 Suppose I have foo which is a populated std::vector<double> . I need to operate on the elements of this vector. I'm motivated to write for (auto it : foo){ /*ToDo - Operate on 'it'*/ } But it appears that this will not write back to foo since it is a value type: a deep copy of the vector element has been taken. Can I give some guidance to auto to make it a reference type? Then I could operate directly on it . I suspect I'm missing some trivial syntax. 回答1: A minimal auto reference The loop