c++14

Can returning a local variable by value in C++11/14 result in the return value being constructed by rvalue when no copy/move is involved?

你离开我真会死。 提交于 2019-12-27 12:25:06
问题 I know that in the following situation that the compiler is free to move-construct the return value from makeA (but is also free to elide the copy or move altogether): struct A { A(A&); A(A&&); }; A makeA() { A localA; return localA; } What I wonder is whether the compiler is allowed to construct an object of type A from a local object of type B by rvalue reference if it is being constructed in the return statement. In other words, in the following example, is the compiler allowed to select A

How to find the most repeated word?

狂风中的少年 提交于 2019-12-25 20:04:22
问题 How can make the program to print the most repeated word? For now it prints as follow: Input: apple banana apple apple Output: apple appeared 3 times banana appeared 1 times int main() { string words; vector<string> stringHolder; while (cin >> words) { stringHolder.push_back(words); } sort(stringHolder.begin(), stringHolder.end()); int vSize = stringHolder.size(); if (vSize == 0) { cout << " no words "; } int wordCount = 1; words = stringHolder[0]; for (int i = 1; i<vSize; i++) { if (words !=

Use enum to determine type of return result ( a hack using Macro )

三世轮回 提交于 2019-12-25 14:01:42
问题 I have many types of game-object that are related together is some ways. All relations is implemented by Map<K1,K2> . #include <vector> using namespace std; template<class K1,class K2> class Map{ //N:N relation public: std::vector<K2*> getK2(K1* k1){/* some code */return std::vector<K2*>();} public: std::vector<K1*> getK1(K2* k2){/* some code */return std::vector<K1*>();} //... various function ... }; Here is the hub class GameRelation that facilitates all relation query :- (just an example,

Use enum to determine type of return result ( a hack using Macro )

雨燕双飞 提交于 2019-12-25 14:01:21
问题 I have many types of game-object that are related together is some ways. All relations is implemented by Map<K1,K2> . #include <vector> using namespace std; template<class K1,class K2> class Map{ //N:N relation public: std::vector<K2*> getK2(K1* k1){/* some code */return std::vector<K2*>();} public: std::vector<K1*> getK1(K2* k2){/* some code */return std::vector<K1*>();} //... various function ... }; Here is the hub class GameRelation that facilitates all relation query :- (just an example,

Create a signal with Gtkmm

别说谁变了你拦得住时间么 提交于 2019-12-25 04:24:11
问题 I am using the gtkmm library with C++, and I am trying to create a signal which allows to change current tab, but it does not work. Actually I think the problem comes from this line: menuit->signal_activate().connect([&bo]() {bo->next_page();}); Where: menuit = Gtk::MenuItem bo = Gtk::Notebook The code compiles well, but when executing I get this line : Segmentation fault (program exited with code: 139) Thank you a lot for your help ! 回答1: menuit->signal_activate().connect([&bo]() {bo->next

class template without public constructor as member of another class template

大城市里の小女人 提交于 2019-12-25 03:52:05
问题 I have a class template Shape , which contains information about certain shapes (which can be three- or two-dimensional). I only want a few predefined shapes (cube, sphere and square) to be available. All these predefined shapes have the same properties (so the cube always has the same volume, and I only need to remember the properties of one cube). To inhibit someone from creating other Shape s, I made the constructor private : // Flag for the possible shapes enum class Tag { SPHERE, CUBE,

How to cause a compile time error based on the size of an initializer_list?

浪子不回头ぞ 提交于 2019-12-25 01:53:05
问题 If I have a simple structure like struct Point { int x, y; }; then I can do int main() { Point p1 = { 10 }; // x = 10, y = 0 Point p2 = { 10, 20 }; // x = 10, y = 20 Point p3 = { 10, 20, 30 }; // Compile error: too many initializers for ‘Point’ return 0; } I now want to have the same behaviour when initializing Point with Point becoming a class but with x and y becoming private and using accessors, etc. My first attempt was class Point { public: Point( std::initializer_list<int> init ) {

Extending Multi patterned variadic templates in C++

拈花ヽ惹草 提交于 2019-12-24 23:06:39
问题 This question is a follow on from my previous question Multi patterned varadic templates in C++ to which I received the solution: #include <array> #include <iostream> #include <type_traits> template <typename T, std::size_t N> class Vec; template <std::size_t, typename ...> struct dimVec; // ground case for no Vecs: unimplemented for SFINAE failure ! template <> struct dimVec<0U>; // ground case with one or more Vecs: size fixed template <std::size_t N> struct dimVec<N> : public std::integral

Constructing std::string using << operator

爱⌒轻易说出口 提交于 2019-12-24 18:02:09
问题 If we want to construct a complex string, say like this: "I have 10 friends and 20 relations" (where 10 and 20 are values of some variables) we can do it like this: std::ostringstream os; os << "I have " << num_of_friends << " friends and " << num_of_relations << " relations"; std::string s = os.str(); But it is a bit too long. If in different methods in your code you need to construct compound strings many times you will have to always define an instance of std::ostringstream elsewhere. Is

Constructing std::string using << operator

☆樱花仙子☆ 提交于 2019-12-24 18:01:23
问题 If we want to construct a complex string, say like this: "I have 10 friends and 20 relations" (where 10 and 20 are values of some variables) we can do it like this: std::ostringstream os; os << "I have " << num_of_friends << " friends and " << num_of_relations << " relations"; std::string s = os.str(); But it is a bit too long. If in different methods in your code you need to construct compound strings many times you will have to always define an instance of std::ostringstream elsewhere. Is