c++17

Correctly propagating a `decltype(auto)` variable from a function

孤街浪徒 提交于 2019-12-20 16:46:40
问题 (This is a follow-up from "Are there any realistic use cases for `decltype(auto)` variables?" ) Consider the following scenario - I want to pass a function f to another function invoke_log_return which will: Invoke f ; Print something to stdout ; Return the result of f , avoiding unnecessary copies/moves and allowing copy elision. Note that, if f throws, nothing should be printed to stdout . This is what I have so far: template <typename F> decltype(auto) invoke_log_return(F&& f) { decltype

std::make_shared() change in C++17

我们两清 提交于 2019-12-20 16:29:10
问题 In cppref, the following holds until C++17: code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g gets called after new int(42) and throws an exception, while f(std::make_shared<int>(42), g()) is safe, since two function calls are never interleaved. I'm wondering which change introduced in C++17 renders this no longer applicable. 回答1: The evaluation order of function arguments are changed by P0400R0. Before the change, evaluation of function arguments are

VS 2017 program not recognizing “scoped_lock”

大憨熊 提交于 2019-12-20 05:31:15
问题 I've been having issues using scoped_locked in VS 2017. I believe I traced them back to the <mutex> declaration, copied below. What would be the safest way to disable the #if switch at the beginning to gain use to scoped_lock? Thanks again. #if _HAS_CXX17 // CLASS TEMPLATE scoped_lock template<class... _Mutexes> class scoped_lock { // class with destructor that unlocks mutexes public: explicit scoped_lock(_Mutexes&... _Mtxes) : _MyMutexes(_Mtxes...) { // construct and lock _STD lock(_Mtxes...

VS 2017 program not recognizing “scoped_lock”

徘徊边缘 提交于 2019-12-20 05:31:01
问题 I've been having issues using scoped_locked in VS 2017. I believe I traced them back to the <mutex> declaration, copied below. What would be the safest way to disable the #if switch at the beginning to gain use to scoped_lock? Thanks again. #if _HAS_CXX17 // CLASS TEMPLATE scoped_lock template<class... _Mutexes> class scoped_lock { // class with destructor that unlocks mutexes public: explicit scoped_lock(_Mutexes&... _Mtxes) : _MyMutexes(_Mtxes...) { // construct and lock _STD lock(_Mtxes...

Is it possible to use structured bindings to assign class members?

我的未来我决定 提交于 2019-12-20 05:26:14
问题 I'd like to use C++ 17 structured bindings to assign a value to a class member variable, like this: #include <cmath> #include <iostream> struct Result { double value; bool error; }; Result square_root(double input) { return {std::sqrt(input), input < 0}; } struct Calculator { double result_; bool error_; public: void ComputeSquareRoot(double input) { [ result_, error_ ] = square_root(input); } void DisplayResult() { if (error_) std::cout << "Cannot take the square root of a negative number.\n

Comparing constexpr function parameter in constexpr-if condition causes error

余生长醉 提交于 2019-12-20 05:13:50
问题 I'm trying to compare a function parameter inside a constexpr-if statement. Here is a simple example: constexpr bool test_int(const int i) { if constexpr(i == 5) { return true; } else { return false; } } However, when I compile this with GCC 7 with the following flags: g++-7 -std=c++1z test.cpp -o test I get the following error message: test.cpp: In function 'constexpr bool test_int(int)': test.cpp:3:21: error: 'i' is not a constant expression if constexpr(i == 5) { return true; } However, if

How can I detect that a constuctor is really constexpr, so I can utilize static initialization?

允我心安 提交于 2019-12-20 04:16:46
问题 Look at this code: struct NonConstexpr { NonConstexpr() { } }; template <typename T> struct Bar { NonConstexpr nonConstexpr; constexpr Bar() { } }; struct Foo { Bar<void> bar; constexpr Foo() { } }; In this code, Foo 's constructor is tagged as constexpr , but it cannot appear in a constant expression, as it actually fails to satisfy the requirements of this. You can read the details of this in my previous question. My question is: can I detect somehow compile-time, that Foo 's constructor

Template argument deduction when the function returns a type composed from the template type and another

假如想象 提交于 2019-12-20 03:08:30
问题 The title is rather hard to formulate in word, but here is what I'm trying to achieve in non-compileable code: template<template <typename> class Container> Container<int> foo() { return Container<int>{1,2,3}; } int main() { auto bar = foo<std::vector>(); return 0; } Basically I want a template function that can "compose" its return type from a type that is passed to it and a previously known type (in this case int ). In this case I want a function that returns an arbitrary data type inside a

C++ how to replace constructor switch?

守給你的承諾、 提交于 2019-12-20 02:56:38
问题 I would like to replace big switch with something more elegant. class Base { public: Base(void*data, int size); virtual void Something() = 0; } class A : public Base { public: A(void*data, int size) : Base(data, size) {} void Something() override; } class B : public Base { public: B(void*data, int size) : Base(data, size) {} void Something() override; } ... { char c = input; switch (c) { case 'a': { A obj(data, size); obj.Something(); break; } case 'b': { B obj(data, size); obj.Something();

Why can't I use operator bool() for std::ofstream

回眸只為那壹抹淺笑 提交于 2019-12-19 17:42:41
问题 Why can't I write the following code? #include <fstream> #include <string> bool touch(const std::string& file_path) { return std::ofstream(file_path, std::ios_base::app); } int main() { touch("foo.txt"); } Output prog.cpp: In function 'bool touch(const string&)': prog.cpp:6:52: error: cannot convert 'std::ofstream {aka std::basic_ofstream<char>}' to 'bool' in return return std::ofstream(file_path, std::ios_base::app); http://ideone.com/IhaRaD I know that std::fstream 's operator bool()