auto


Return type deduction for in-class friend functions

大城市里の小女人 提交于 2020-01-21 02:36:10
问题 Here is a little experiment with return type deduction for in-class friend functions (using Clang 3.4 SVN and g++ 4.8.1 with std=c++1y in both cases) that is not documented in the linked working paper #include <iostream> struct A { int a_; friend auto operator==(A const& L, A const& R) { return L.a_ == R.a_; // a_ is of type int, so should return bool } }; template<class T> struct B { int b_; friend auto operator==(B const& L, B const& R) { return L.b_ == R.b_; // b_ is of type int, so should

Why would they special-case certain initializer lists instead of treating them all the same?

倖福魔咒の 提交于 2020-01-13 14:12:10
问题 Say I have a variable auto x that I want to initialize to 7 using brace initialization, simple: auto x {7}; Except I learned that x is NOT an integer, but an initialization list itself. Why? Is there a specific reason why the committee would decide that auto should grab the initialization list in the case of a single auto value, or do they expect us to just realize these shouldn't be used together. I cant seem to think of a possible reason i would want an initializer list to be stored into

Compiler can't deduce the return type?

会有一股神秘感。 提交于 2020-01-11 00:57:50
问题 I am trying to use the decltype keyword on an auto function: struct Thing { static auto foo() { return 12; } using type_t = decltype(foo()); }; And I get the following error (gcc 7.4): <source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto' decltype(foo()); ^ <source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto' Why has the compiler not yet deduced the return type? 回答1: Because for class definition, compiler will first determine all

Why does a range-based for statement take the range by auto&&?

三世轮回 提交于 2020-01-10 17:48:40
问题 A range-based for statement is defined in §6.5.4 to be equivalent to: { auto && __range = range-init; for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) { for-range-declaration = *__begin; statement } } where range-init is defined for the two forms of range-based for as: for ( for-range-declaration : expression ) => ( expression ) for ( for-range-declaration : braced-init-list ) => braced-init-list (the clause further specifies the meaning of the other sub

Type of an auto initialized list

僤鯓⒐⒋嵵緔 提交于 2020-01-04 06:27:09
问题 In the C++ code below, what is type of a ? typeid returns St16initializer_listIPKcE auto a = { "lol", "life" }; 回答1: When you have auto a = { "lol", "life" }; The compiler will try to deduce a std::initializer_list where the type is what all of the elements are. In this case "lol" and "life" are both a const char[] so you have a std::initializer_list<const char*> . If on the other have you had something like auto foo = { 1, 2.0 }; Then you would have a compiler error since the element types

What is the type of “auto var = {condition} ? 1 : 1.0” in C++11? Is it double or int?

廉价感情. 提交于 2020-01-02 02:22:10
问题 In C++11 what are the types of x and y when I write this? int main() { auto x = true ? 1 : 1.0; auto y = false ? 1 : 1.0; std::cout << x << endl; std::cout << y << endl; return 0; } 回答1: The type is going to be double , because it's the common type of the literals 1 and 1.0 . There's a simple way to test that using typeid : #include <iostream> #include <typeinfo> using namespace std; int main() { auto x = true ? 1 : 1.0; cout << typeid(x).name() << endl; return 0; } This outputs d on my

Why is direct-list-initialization with auto considered bad or not preferred?

我与影子孤独终老i 提交于 2020-01-01 08:50:09
问题 I've come into the habit of writing code with direct-list-initialization like below as it's more effective and it's very useful to prevent implicit narrowing: int i {0}; string s {""}; char c {'a'}; bool b {false}; auto num {100}; // But this?? But when it comes to the auto specifier, I have heard it is considered bad or not preferred to write it like that, why is that? 回答1: Here's an example of where using that syntax fails: struct Foo{}; void eatFoo (const Foo& f){} int main() { Foo a; auto

Can i use auto or decltype instead trailing return type?

狂风中的少年 提交于 2020-01-01 01:12:29
问题 I find trailing return type so easy to define the return of a function that returns a complicated types e.g: auto get_diag(int(&ar)[3][3])->int(&)[3]{ // using trailing return type static int diag[3]{ ar[0][0], ar[1][1], ar[2][2] }; return diag; } auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer static int diag[3]{ ar[0][0], ar[1][1], ar[2][2] }; return diag; } int main(){ int a[][3]{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; decltype(get_diag(a))

Using 'auto' type deduction - how to find out what type the compiler deduced?

醉酒当歌 提交于 2019-12-31 08:28:26
问题 How can I find out what type the compiler deduced when using the auto keyword? Example 1: Simpler auto tickTime = 0.001; Was this deduced as a float or a double? Example 2: More complex (and my present headache): typedef std::ratio<1, 1> sec; std::chrono::duration<double, sec > timePerTick2{0.001}; auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2; What type is nextTickTime ? The problem I'm having is when I try to send nextTickTime to std::cout . I get the

C++11 auto: what if it gets a constant reference?

耗尽温柔 提交于 2019-12-27 13:39:47
问题 Please take a look at the following simple code: class Foo { public: Foo(){} ~Foo(){} Foo(const Foo&){} Foo& operator=(const Foo&) { return *this; } }; static Foo g_temp; const Foo& GetFoo() { return g_temp; } I tried to use auto like this: auto my_foo = GetFoo(); I expected that my_foo will be a constant reference to Foo , which is the return type of the function. However, the type of auto is Foo , not the reference. Furthermore, my_foo is created by copying g_temp . This behavior isn't that

工具导航Map