c++14

Are implementations allowed to add public members to standard types?

放肆的年华 提交于 2019-12-03 11:42:31
Are C++ standard library implementations allowed to add public (and protected) members to standard types' interfaces? N3797 17.6.5.5 [member.functions]/2 says: An implementation may declare additional non-virtual member function signatures within a class: — by adding arguments with default values to a member function signature; [ Note : An implementation may not add arguments with default values to virtual, global, or non-member functions. — end note ] — by replacing a member function signature with default values by two or more member function signatures with equivalent behavior; and — by

Cannot construct constexpr array from braced-init-list

[亡魂溺海] 提交于 2019-12-03 11:40:02
问题 I've implemented a constexpr array like this: template <typename T> class const_array { const T* p; unsigned n; public: template <unsigned N> constexpr const_array(const T(&a)[N]): p(a), n(N) { } constexpr unsigned size() const { return n; } }; int main(int argc, char* argv[]) { // works static_assert(const_array<double>{{1.,2.,3.}}.size() == 3); // doesn't compile constexpr const_array<double> a{{1.,2.,3.}}; static_assert(a.size() == 3); } Why is it that the first static_assert compiles, but

CRTP and c++1y return type deduction

感情迁移 提交于 2019-12-03 11:37:35
I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template<typename Derived> struct Base { auto foo() { return static_cast<Derived*>(this)->foo_impl(); } }; struct Derived: public Base<Derived> { auto foo_impl() -> int { return 0; } }; int main() { Derived b; int i = b.foo(); (void)i; } I assumed that the return type from Base<Derived>::foo was a decltype of the expression returned, but if I modify the functio foo like this: auto foo() -> decltype(static_cast<Derived*>(this)->foo_impl(

What is this C++14 construct called which seems to chain lambdas?

本小妞迷上赌 提交于 2019-12-03 11:36:46
This is a follow-up question on this one: Lambda-Over-Lambda in C++14 , where the answers explain the code. It is about a lambda that creates another lambda which when called, calls the passed lambda and passes the return value to the original lambda, thus returning a new instance of the second lambda. The example shows how this way lambdas can be chained. Copy from the original question: #include <cstdio> auto terminal = [](auto term) // <---------+ { // | return [=] (auto func) // | ??? { // | return terminal(func(term)); // >---------+ }; }; auto main() -> int { auto hello =[](auto s){

Has there been a proposal to add std::bin to the c++ standard?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:33:58
问题 C++14 adds ability to use binary literals by typing 0b prefix for the value: int v = 0b1111; // 15 in decimal But there is no std::bin manipulator for streams like std::hex or std::oct . So I need to use e.g. std::bitset for printing purpose: std::cout << std::bitset<4>(v) << "\n"; Has it been proposed or considered? If so, what's the status of the idea? 回答1: As far as I know there was no proposal submitted to add a formatting flag to add binary formatting and/or a manipulator std::bin . You

Which standard C++ features can be used for querying machine/OS architecture?

跟風遠走 提交于 2019-12-03 11:33:18
问题 What are the standard C++ features and utilities for querying the properties of the hardware or operating system capabilities, on which the program is running? For instance, std::thread::hardware_concurrency() gives you the number of threads the machine supports. But how do you detect how much RAM the computer has, or how much RAM the process is using, or how much disk space is available to write to in a certain directory, or how much L2 cache is available? I would prefer answers by means of

Is it legal to check whether the address of a subobject lies within the bounds of a containing object

纵然是瞬间 提交于 2019-12-03 11:11:51
2 Questions: Is the following code well formed with defined behaviour? Is there any possible c++ implementation in which it could assert? Code (c++11 and higher): #include <cassert> #include <utility> #include <ciso646> template<class T> auto to_address(T* p) { return reinterpret_cast<unsigned char const*>(p); } /// Test whether part is a sub-object of object template<class Object, class Part> bool is_within_object(Object& object, Part& part) { auto first = to_address(std::addressof(object)), last = first + sizeof(Object); auto p = to_address(std::addressof(part)); return (first <= p) and (p <

How will C++17 exception specifier type system work?

一笑奈何 提交于 2019-12-03 11:07:12
问题 Studying about "noexcept specifier(and operator)", I wrote a simple code. And I am surprised that this piece of code: void asdf() noexcept {} int main() { auto f = asdf; std::cout << std::boolalpha << noexcept(f()) << std::endl; } prints false , even function "asdf" is noexcept-specified. So while searching why this mysterious phenomenon is happening, I found C++17's "exception specifier type system"- P0012R1. According to this (accepted) proposal, since C++17; as noexcept is part of function

Why doesn't my templated function promote 'int' to 'T', where 'T' = 'double'?

浪尽此生 提交于 2019-12-03 11:02:14
问题 I have a class templated with typename T . It contains a function, template <typename T, size_t a> myClass<T,a> operator+(myClass<T,a> lhs, const T& rhs) { return lhs += rhs; } myClass<T,a> myClass<T,a>::operator+=(const T& rhs) { // Do addition, depends on 'a'. return *this; } When I call this with, for example myClass<double, 2> myObj_double_2(constructor args); myObj_double_2 = myObj_double_2 + 5.2; I have no problem. If I however call myObj_double_2 = myObj_double_2 + 5; Then the compiler

Use of 'auto func(int)' before deduction of 'auto' in C++14

随声附和 提交于 2019-12-03 10:50:14
问题 I have compiled following program in GCC using C++14 . #include <iostream> using namespace std; auto func(int i); int main() { auto ret = func(5); return 0; } auto func(int i) { if (i == 1) return i; else return func(i-1) + i; } But, I get the following error. In function 'int main()': 8:16: error: use of 'auto func(int)' before deduction of 'auto' auto ret = func(5); So, what am I missing here? 回答1: This is [dcl.spec.auto/11]: If the type of an entity with an undeduced placeholder type is