auto

Variable template in template class - unexpected error (possible bug?)

和自甴很熟 提交于 2019-11-30 11:22:19
Having: struct Value { template<class T> static constexpr T value{0}; }; (0) ideone template<typename TValue> struct Something { void x() { static_assert(TValue::template value<int> == 0, ""); } }; int main() { Something<Value>{}.x(); return 0; } Does not compile with clang++ 3.6. error: cannot refer to variable template 'value' without a template argument list Does not compile with g++ 5.2. error: ‘template constexpr const T Value::value’ is not a function template (1) ideone Compiles with both clang++ and g++. struct Something { void x() { static_assert(Value::template value<int> == 0, "");

Why does decltype(auto) return a reference here?

↘锁芯ラ 提交于 2019-11-30 11:02:41
问题 I think (thought) I understand auto . Same about decltype . However, in C++14, one can have some diabolic thing like decltype(auto) as the return type of a function. Consider the following: decltype(auto) foo() { int m = 1; return m; } The return type is int , everything makes sense. However, decltype(auto) foo() { int m = 1; return (m); } returns int& (i.e. reference to int ). I have absolutely NO IDEA why this happens, why do these parentheses make any difference at all!? Hope someone can

Return type deduction for in-class friend functions

走远了吗. 提交于 2019-11-30 10:50:35
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 return bool } }; using BI = B<int>; int main() { std::cout << (A{1} == A{2}) << "\n"; // OK for Clang,

css:css认知总结

两盒软妹~` 提交于 2019-11-30 10:25:52
什么是css? css英文全称Cascading Style Sheets,中文全称层叠样式表,作用是编辑、排版、控制html的样式。 选择器(普通选择器;伪类选择器;伪元素选择器) 属性选择器: e[att^=’val’]{}:某属性开头包含某字符串 e[att$=’val’]{}:某属性结尾包含某字符串 e[att*=’val’]{}:某属性包含某字符串 结构性伪类选择器 :root{}:根元素选择器,控制html(推荐使用) input:not([type=’submit’]){}:否定选择器,选择除此以外的其他元素 Div:empty{}:空元素选择器,选择没有任何内容的元素,包括空格 :target{} ul>li:first-child {color: red;} :子元素选择器,不是后代选择器 ul>li:last-child {color: red;} :子元素选择器,不是后代选择器 div > div:nth-child(2){}:选择一个或多个子元素(2,2n,odd,even) div > div:nth-last-child(2){}:同上,但是从最后一个子元素开始计算 div > div:nth-of-type(5){} :指定某类子元素的某个或多个 div > div:nth-last-of-type(5){}:同上,但是从最后一个子元素开始计算 div

'auto' not allowed in function prototype with Clang

纵然是瞬间 提交于 2019-11-30 08:00:42
问题 Using Clang 3.5, 3.6, or 3.7, with the flag std=c++1y the following code does not compile : #include <iostream> auto foo(auto bar) { return bar; } int main() { std::cout << foo(5.0f) << std::endl; } The error given is : error: 'auto' not allowed in function prototype I do not have errors using g++ 4.9. Is this error produced because Clang has not yet implemented this functionnality yet or is it because I am not allowed to do that and GCC somehow permits it ? 回答1: As we see from the ISO C++

Is 'auto const' and 'const auto' the same?

北慕城南 提交于 2019-11-30 07:48:14
Is there a difference or is it the same? The const qualifier applies to the type to the immediate left unless there is nothing to the left then it applies to the type to the immediate right. So yup it's the same. AndiDog Contrived example: std::vector<char*> test; const auto a = test[0]; *a = 'c'; a = 0; // does not compile auto const b = test[1]; *b = 'c'; b = 0; // does not compile Both a and b have type char* const . Don't think you can simply "insert" the type instead of the keyword auto (here: const char* a )! The const keyword will apply to the whole type that auto matches (here: char* )

C++ auto& vs auto

∥☆過路亽.° 提交于 2019-11-30 06:11:07
问题 When creating local variables, is it correct to use (const) auto& or auto ? e.g.: SomeClass object; const auto result = object.SomeMethod(); or const auto& result = object.SomeMethod(); Where SomeMethod() returns a non-primitive value - maybe another user-defined type. My understanding is that const auto& result is correct since the result returned by SomeMethod() would call the copy constructor for the returned type. Please correct me if I am wrong. What about for primitive types? I assume

Why do I need to explicitly write the 'auto' keyword?

懵懂的女人 提交于 2019-11-30 02:32:53
I am moving towards C++11 from C++98 and have become familiar with the auto keyword. I was wondering why we need to explicitly declare auto if the compiler is able to automatically deduce the type. I know C++ is a strongly typed language and this is a rule but was it not possible to achieve the same outcome without explicitly declaring a variable auto ? Dropping the explicit auto would break the language: e.g. int main() { int n; { auto n = 0; // this shadows the outer n. } } where you can see that dropping the auto would not shadow the outer n . Your question allows two interpretations: Why

Why does decltype(auto) return a reference here?

和自甴很熟 提交于 2019-11-29 23:01:23
I think (thought) I understand auto . Same about decltype . However, in C++14, one can have some diabolic thing like decltype(auto) as the return type of a function. Consider the following: decltype(auto) foo() { int m = 1; return m; } The return type is int , everything makes sense. However, decltype(auto) foo() { int m = 1; return (m); } returns int& (i.e. reference to int ). I have absolutely NO IDEA why this happens, why do these parentheses make any difference at all!? Hope someone can shed some light on this. PS: I've also tagged with C++ as there are many more people that check the C++

A lambda's return type can be deduced by the return value, so why can't a function's?

南笙酒味 提交于 2019-11-29 21:11:25
#include <iostream> int main(){ auto lambda = [] { return 7; }; std::cout << lambda() << '\n'; } This program compiles and prints 7. The return type of the lambda is deduced to the integer type based on the return value of 7. Why isn't this possible with ordinary functions? #include <iostream> auto function(){ return 42; } int main(){ std::cout << function() << '\n'; } error: ‘function’ function uses ‘auto’ type specifier without trailing return type Mark Garcia C++14 has this feature . You can test it with new versions of GCC or clang by setting the -std=c++1y flag. Live example In addition