c++14

Force compiler to choose copy constructor with const T& as a parameter

北城余情 提交于 2020-02-27 23:17:30
问题 I'm writing a class where I have a templated constructor and copy constructor. Every time I want to call copy constructor with non const object, templated constructor gets chosen. How can I force compiler to choose copy constructor? Here is the mcve: #include <iostream> struct foo { foo() { std::cout << "def constructor is invoked\n"; } foo(const foo& other) { std::cout << "copy constructor is invoked\n"; } template <typename T> foo(T&& value) { std::cout << "templated constructor is invoked

default override of virtual destructor

只愿长相守 提交于 2020-02-26 07:38:01
问题 Everyone knows that the desructor of base class usually has to be virtual. But what is about the destructor of derived class? In C++11 we have keyword "override" and ability to use the default destructor explicitly. struct Parent { std::string a; virtual ~Parent() { } }; struct Child: public Parent { std::string b; ~Child() override = default; }; Is it correct to use both keywords "override" and "=default" in the destructor of Child class? Will compiler generate correct virtual destructor in

exception specification of overriding function is more lax than base version

三世轮回 提交于 2020-02-23 09:14:40
问题 I want to custom an Exception class, here's the code: class TestException : std::exception{ public: const char *what() const override { return "TestException"; } }; I used Clion and the IDE give me a warning on the function what() : exception specification of overriding function is more lax than base version But if I build the code with gcc, there's no warning came out. I used c++ 14, gcc 6.5.0 Can anybody help to explain what does the warning mean and can I just ignore it? 回答1: what from std

c++: error: unrecognized command line option ‘-std=c++14’

一世执手 提交于 2020-02-22 08:31:06
问题 I just moved my PC from Ubuntu 15.10 to Linux Mint 17.3. Before this shift, this project compiled just fine in CLion. Now, it gives the following error: c++: error: unrecognized command line option ‘-std=c++14’ This is probably due to this line in my CMake file: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -W -Wall -Wextra -pedantic") I suspect this error is due to some outdated library/compiler. I don't know exactly what I need to do. P.S.: I know that C++14 is not completely supported

Ok to use std::getline() with a moved-from std::string?

前提是你 提交于 2020-02-21 10:35:52
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Ok to use std::getline() with a moved-from std::string?

别来无恙 提交于 2020-02-21 10:31:44
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Ok to use std::getline() with a moved-from std::string?

烂漫一生 提交于 2020-02-21 10:28:07
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Ok to use std::getline() with a moved-from std::string?

天大地大妈咪最大 提交于 2020-02-21 10:27:07
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Execute function inside function template only for those types that have the function defined

核能气质少年 提交于 2020-02-21 09:55:30
问题 I have a function template which takes many different types as it's input. Out of those types only one of them has a getInt() function. Hence I want the code to run the function only for that type. Please suggest a solution. Thanks #include <type_traits> #include <typeinfo> class X { public: int getInt(){ return 9; } }; class Y{ }; template<typename T> void f(T& v){ // error: 'class Y' has no member named 'getInt' // also tried std::is_same<T, X>::value if(typeid(T).name() == typeid(X).name()

Moving a lambda: once you've move-captured a move-only type, how can the lambda be used? [duplicate]

徘徊边缘 提交于 2020-02-12 11:53:16
问题 This question already has answers here : How to create an std::function from a move-capturing lambda expression? (3 answers) Closed 4 years ago . This answer explains how to move-capture a variable within a lambda in C++14. But once you've move-captured an un-copyable object (such as a std::unique_ptr ) within a lambda, you cannot copy the lambda itself. This would be fine if you could move the lambda, but I get a compile error when trying to do so: using namespace std; class HasCallback {