c++17

With guaranteed copy elision, why does the class need to be fully defined?

ぐ巨炮叔叔 提交于 2019-12-02 20:28:05
A followup to this post . Consider the following: class C; C foo(); That is a pair of valid declarations. C doesn't need to be fully defined when merely declaring a function. But if we were to add the following function: class C; C foo(); inline C bar() { return foo(); } Then suddenly C needs to be a fully defined type. But with guaranteed copy elision, none of its members are required. There's no copying or even a move, the value is initialized elsewhere, and destroyed only in the context of the caller (to bar ). So why? What in the standard prohibits it? The rule lies in [basic.lval]/9 :

std::any without RTTI, how does it work?

大憨熊 提交于 2019-12-02 20:24:41
If I want to use std::any I can use it with RTTI switched off. The following example compiles and runs as expected also with -fno-rtti with gcc. int main() { std::any x; x=9.9; std::cout << std::any_cast<double>(x) << std::endl; } But how std::any stores the type information? As I see, if I call std::any_cast with the "wrong" type I got std::bad_any_cast exception as expected. How is that realized or is this maybe only a gcc feature? I found that boost::any did also not need RTTI, but I found also not how that is solved. Does boost::any need RTTI? . Digging into the STL header itself gives me

How to make a safer C++ variant visitor, similar to switch statements?

时光怂恿深爱的人放手 提交于 2019-12-02 20:14:50
The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: ( snippet from cppreference.com ) std::variant<int, long, double, std::string> v = ...; std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); The problem is when you put the wrong type in the visitor or change the variant signature, but forget to change the visitor. Instead of getting a compile error, you will have the wrong

How to get the file size in bytes with C++17

时光怂恿深爱的人放手 提交于 2019-12-02 19:52:47
Are there pitfalls for specific operating systems, I should know of? There are many duplicates ( 1 , 2 , 3 , 4 , 5 ) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today. Methods from other (old QA's) on .sx stat.h (wrapper sprintstatf ), uses syscall tellg() , returns per definition a position but not necessarily bytes . The return type is not int . <filesystem> (added in C++17) makes this very straightforward . #include <cstdint> #include <filesystem> // ... std::uintmax_t size = std::filesystem::file_size("c:\\foo\\bar

Exact moment of “return” in a C++-function

若如初见. 提交于 2019-12-02 19:48:31
It seems like a silly question, but is the exact moment at which return xxx; is "executed" in a function unambiguously defined? Please see the following example to see what I mean ( here live ): #include <iostream> #include <string> #include <utility> //changes the value of the underlying buffer //when destructed class Writer{ public: std::string &s; Writer(std::string &s_):s(s_){} ~Writer(){ s+="B"; } }; std::string make_string_ok(){ std::string res("A"); Writer w(res); return res; } int main() { std::cout<<make_string_ok()<<std::endl; } What I naively expect to happen, while make_string_ok

Do we need to use std::launder when doing pointer arithmetic within a standard-layout object (e.g., with offsetof)?

纵然是瞬间 提交于 2019-12-02 18:58:13
This question is a follow-up to: Is adding to a "char *" pointer UB, when it doesn't actually point to a char array? In CWG 1314 , CWG affirmed that it is legal to perform pointer arithmetic within a standard-layout object using an unsigned char pointer. This would appear to imply that some code similar to that in the linked question should work as intended: struct Foo { float x, y, z; }; Foo f; unsigned char *p = reinterpret_cast<unsigned char*>(&f) + offsetof(Foo, z); // (*) *reinterpret_cast<float*>(p) = 42.0f; (I have replaced char with unsigned char for greater clarity.) However, it seems

Why am I still getting undefined reference errors linking a static library with CMake in Android NDK?

偶尔善良 提交于 2019-12-02 18:39:36
问题 When building the following Android-NDK project I am getting dozens of undefined reference errors regarding missing standard library functions. I followed some basic examples of linking static libraries and restarted this project from scratch 3 times but I still can't find the issue. I am trying to use the lib_seal library that I compiled using -std=c++1z . (https://www.microsoft.com/en-us/research/project/simple-encrypted-arithmetic-library/). The errors I am getting indicate the library is

Make a function accepting an optional to accept a non-optional?

荒凉一梦 提交于 2019-12-02 17:24:37
I'm trying to write syntactic sugar, in a monad-style, over std::optional . Please consider: template<class T> void f(std::optional<T>) {} As is, this function cannot be called with a non-optional T 1 (e.g. an int ), even though there exists a conversion from T to std::optional<T> 2 . Is there a way to make f accept an std::optional<T> or a T (converted to an optional at the caller site), without defining an overload 3 ? 1) f(0) : error: no matching function for call to 'f(int)' and note: template argument deduction/substitution failed , ( demo ). 2) Because template argument deduction doesn't

Is std::memcpy between different trivially copyable types undefined behavior?

南楼画角 提交于 2019-12-02 17:23:38
I've been using std::memcpy to circumvent strict aliasing for a long time. For example, inspecting a float , like this : float f = ...; uint32_t i; static_assert(sizeof(f)==sizeof(i)); std::memcpy(&i, &f, sizeof(i)); // use i to extract f's sign, exponent & significand However, this time, I've checked the standard, I haven't found anything that validates this. All I found is this : For any object (other than a potentially-overlapping subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes ([intro.memory]) making up the object can

How to efficiently get a `string_view` for a substring of `std::string`

爱⌒轻易说出口 提交于 2019-12-02 17:23:11
Using http://en.cppreference.com/w/cpp/string/basic_string_view as a reference, I see no way to do this more elegantly: std::string s = "hello world!"; std::string_view v = s; v = v.substr(6, 5); // "world" Worse, the naive approach is a pitfall and leaves v a dangling reference to a temporary: std::string s = "hello world!"; std::string_view v(s.substr(6, 5)); // OOPS! I seem to remember something like there might be an addition to the standard library to return a substring as a view: auto v(s.substr_view(6, 5)); I can think of the following workarounds: std::string_view(s).substr(6, 5); std: