auto

equivalence between decltype and auto

一曲冷凌霜 提交于 2019-12-24 09:15:29
问题 Since auto and decltype are both used to infer the types. I thought they would be same. However, the answer to this question suggests otherwise. Still I think they cannot be entirely different. I can think of a simple example where the type of i will be same in both the following cases. auto i = 10; and decltype(10) i = 10; So what are the possible situations where auto and decltype would behave equivalently. 回答1: auto behaves exactly the same as template argument deduction, meaning if you

C++ 存储类

旧城冷巷雨未停 提交于 2019-12-24 04:42:27
存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期。这些说明符放置在它们所修饰的类型之前。下面列出 C++ 程序中可用的存储类: auto register static extern mutable thread_local (C++11) 从 C++ 11 开始,auto 关键字不再是 C++ 存储类说明符,且 register 关键字被弃用。 auto 存储类 自 C++ 11 以来, auto 关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型、声明函数时函数返回值的占位符。 C++98标准中auto关键字用于自动变量的声明,但由于使用极少且多余,在C++11中已删除这一用法。 根据初始化表达式自动推断被声明的变量的类型,如: auto f=3.14; //double auto s("hello"); //const char* auto z = new auto(9); // int* auto x1 = 5, x2 = 5.0, x3='r';//错误,必须是初始化为同一类型 register 存储类 register 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 '&' 运算符(因为它没有内存位置)。 { register int miles; }

Does the 'auto' keyword know when to use a const iterator?

女生的网名这么多〃 提交于 2019-12-24 03:54:10
问题 If you're looping through a container as such: typedef std::vector<std::unique_ptr<BaseClass>> Container; Container container; for(Container::const_iterator element = container.begin(); element != container.end(); element++) { //Read through values } And instead of using the typedef you decide to use auto: std::vector<std::unique_ptr<BaseClass>> container; for(auto element = container.begin(); element != container.end(); element++) { //Read through values } Assuming you don't alter the values

Can global auto variables be declared in h files? [duplicate]

会有一股神秘感。 提交于 2019-12-24 02:14:21
问题 This question already has an answer here : extern auto variable has no initializer (1 answer) Closed 4 months ago . Somewhat similar to this post, but still different: can I define a global auto variable in some header file? I tried using the following files, but couldn't make them compile. $ cat main.cpp auto a = 5; #include "defs.h" int main(int argc, char **argv){ return a; } $ cat defs.h #ifndef __DEFS_H__ #define __DEFS_H__ extern auto a; #endif And after standard compilation ( g++ main

clang++ auto return type error for specialization of templated method in templated class?

橙三吉。 提交于 2019-12-24 01:13:01
问题 Trying to understand another question, I've simplified the example obtaining the following code. template <bool> struct foo { template <typename T> auto bar (int i) { return i; } }; template <> template <typename T> auto foo<true>::bar (int i) { return i; } int main() { return 0; } g++ 4.9.2 compile it without problem; clang++ 3.5 give the following error tmp_003-14,gcc,clang.cpp:12:20: error: out-of-line definition of 'bar' does not match any declaration in 'foo<true>' auto foo<true>::bar

Why “auto” is not acceptable as lambda parameter

穿精又带淫゛_ 提交于 2019-12-23 19:14:31
问题 Why does this code make a compile error? std::find_if(std::begin(some_list), std::end(some_list), [](const auto& item){ //some code }); The error of course at "auto"? why is not possible to know the type automatically ? thanks 回答1: This is because as of C++11, lambda functions in C++ cannot be defined generically, therefore you cannot declare a parameter using auto . This has been added in the C++14 (and is already supported by some compilers). However, you can achieve the same thing in C++11

How get non-const iterator

不打扰是莪最后的温柔 提交于 2019-12-23 13:11:25
问题 std::map<char,int> dict; ... auto pmax = dict.begin(); // here i get const iterator Can I "explicitly indicate" that the value obtained is a non-constant type? 回答1: Looking at your code, you are basically implementing std::max_element . So you could rewrite your last output line to: std::cout << std::max_element(begin(dict), end(dict), [](decltype(*begin(dict)) a, decltype(*begin(dict)) b) { return a.second < b.second; })->first << std::endl; Admittedly, the decltype(*begin(dict)) is ugly,

Why old usage (c++03) of auto does not compile under C++11?

谁说胖子不能爱 提交于 2019-12-23 12:47:56
问题 I know that auto has a little usage before because it is the default for variables (opposite to static) - see question Consider however valid C++03 code where, maybe for self-explanatory, this keyword was used: auto int foo2 = 8; It compiles under C++03, and does not compile under C++11. Is there any reason for not being back-compatible with C++03? What was the source of standard committee opinion that this keyword was not used? Are there any statistics of keyword usage? BTW i tested with gcc

is std::function heavier than auto storing lambda function

守給你的承諾、 提交于 2019-12-23 10:02:41
问题 I heard that cost of std::function is heavier than auto to deal with a lambda function. effective modern c++ item5. What I want is to clarify the mechanism why std::function use more memory than auto with some sample code. Could somebody help me? edit class Widget { public: Widget(int i) : i_(i) {} bool operator<(const Widget& o) { return o.value() > i_; } int value() const { return i_; }; private: int i_; int dummy_[1024]; }; int main() { // performance difference between auto and std:

C++11 type deduction vs const char *

Deadly 提交于 2019-12-23 07:46:10
问题 In GotW 94, Herb Sutter draws a distinction between the "classic C++" declaration const char* s = "Hello"; and the "modern" style auto s = "Hello"; He tells us that there's a "subtle difference in the type of s , where the auto style is more correct". [Edited to add: comments suggest that this might not be a fair representation of what Sutter actually meant; see discussion below.] But... what's the difference? I was under the impression that a const char * is the correct way to refer to a