Invoking the emplace() method for std::vector
What is the correct way to invoke the emplace()
method for std::vector
, given the following template declarations? The requirement is to emplace an element inside the vector impl_tree
at a given position, after the construction of the tree
class template object.
(Note only relevant source code has been reproduced)
template<typename T1>
class node{
public:
node(T1&);
private:
T1 data;
};
template typename<T1,T2>
class tree{
private:
std::vector<std::shared_ptr<node<T1>>> impl_tree;
};
See also questions close to this topic

How can I search for a match through a vector of objects?
I am building up a program where I create a vector of objects, the input variables that create the object are read from a .txt file. I want to check if the "ID" of the object introduced by the user exists in order to continue.
vector<Producto> vectorProductos; while(file >> sID >> sDesc >> sUMed >> sFam >> sClass >> dVolumen >> dLongitud >> sPrecio){ vectorProductos.push_back(Producto(sID, sDesc, sUMed,sFam, sClass, dVolumen, dLongitud, stringToDouble(sPrecio))); iNumProductos++; } file.close(); int iNumPartidas; cout << "iNumPartidas? " << endl; cin >> iNumPartidas; for(unsigned int iP = 1; iP <= iNumPartidas; iP++){ cout << endl << "Partida " << iP << ":" << endl; cout << "Clave de partida:\t"; cin >> sPartida; for(unsigned int iPrd = 0; iPrd < iNumProductos; iPrd++){ cout << endl << "Clave de producto " << iPrd+1 << ":\t"; cin >> sClave; if(sClave == vectorProductos[iPrd].getClave()){ cout << endl << "Cantidad:\t"; cin >> iCantProdxP; }else{ cout << "Producto no existe" << endl; } } }
Producto
class#ifndef Producto_h #define Producto_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Producto{ protected: string _sClave, _sDescripcion, _sUMed, _sClass, _sFam; double _dVolumen, _dPrecio, _dLongitud; public: Producto(); Producto(string, string, string, string, string, double, double, double); inline string getClave(){return _sClave;} inline string getDescripcion(){return _sDescripcion;} inline string getUMed(){return _sUMed;} inline string getFam(){return _sFam;} inline string getClass(){return _sClass;} inline double getVol(){return _dVolumen;} inline double getPrecio(){return _dPrecio;} inline double getLongitud(){return _dLongitud;} inline void setClave(string sClave){_sClave = sClave;} inline void setDescripcion(string sDescripcion){_sDescripcion = sDescripcion;} inline void setUMed(string sUMed){_sUMed = sUMed;} inline void setFam(string sFam){_sFam = sFam;} inline void setClass(string sClass){_sClass = sClass;} inline void setVol(double dVolumen){_dVolumen = dVolumen;} inline void setPrecio(double dPrecio){_dPrecio = dPrecio;} inline void setLongitud(double dLongitud){_dLongitud = dLongitud;} void toString(); }; Producto::Producto(){ _sClave = ""; _sDescripcion = "Falta descripcion"; _sUMed = "N/A"; _sFam = "Sin Familia"; _sClass = "Sin clase"; _dVolumen = 0.0; _dPrecio = 0.0; _dLongitud = 0.0; } Producto::Producto(string sClave, string sDescripcion, string sUMed, string sFam, string sClass, double dVolumen, double dLongitud, double dPrecio){ _sClave = sClave; _sDescripcion = sDescripcion; _sUMed = sUMed; _sFam = sFam; _sClass = sClass; _dVolumen = dVolumen; _dPrecio = dPrecio; _dLongitud = dLongitud; } void Producto::toString(){ cout << "\nProducto: " << _sClave; cout << "\nDescripcion: " << _sDescripcion; cout << "\nUnidad de Medida: " << _sUMed; cout << "\nFamilia: " << _sFam; cout << "\nClase: " << _sClass; cout << "\nVolumen: " << _dVolumen; cout << "\nLongitud: " << _dLongitud; cout << "\nPrecio: " << _dPrecio; cout << endl; }
What I need is to see if that "ID" that the user is going to input actually exists and if not mark error. When I run my program I have to type in the ID of the first product to match it with the index number of the loop, and that is the only way it works but I need the program to match it with any "Producto" regardless of the position or index.

Calculating velocity for points rotating around axis
I am trying to get objects to rotate around the origin.
This is in 3D, so Y is the up axis. They rotate around the origin of the (X,Z) plane
The objects can be anywhere within a circle around the origin. To get their velocity vector I work out the angle between the origin and the object and add 90 degrees to the angle (so all points face a right angle to the origin).
Here is my current code
radians:=ArcTan2(z,x); //add 90 degrees to angle so point is heading at a 90 degree angle around the origin radians:=radians+DegToRad(90); //find new (x2,z2) point x2:=x+cos(radians)*initialspeedscale; z2:=z+sin(radians)*initialspeedscale; //vectors from rotated position to object position vx:=x2x; vz:=z2z;
vx and vz are then used as the initial object velocity.
This is how I think the code should work https://ibb.co/4TPtLtv
The problem is the speeds are not constant. If I scale the speeds of the initial setup between red and blue, I get this image https://ibb.co/Xk56VY0 It must be something with the radians to degrees or arctan2 quadrants but I cannot get this working.
Any ideas on where I have gone wrong?

C++ loop with try/catch doesnt stop after "break"
so I wanna read a file into my vector of my own class with a loop, and in case of exception, catch and ignore them. The problem i keep on having is that it doesnt break when the input file is empty, i only have 9 rows of input but it still keeps running, and it wont print my vector after it either..
int main(int argc, const char* argv[]) { string fname = (argc > 1 ? argv[1] : "input.txt"); ifstream ifs { fname }; if (!ifs.is_open()) throw runtime_error { "cannot open input file " + fname }; vector < ClosedInterval > intervalle; int line = 0; for (ClosedInterval i;; ++line) { try { if (!(ifs >> i)) break; intervalle.push_back(i); } catch (const exception& e) { cerr << "Error line " << line << ": " << e.what() << "\n\tLine ignored\n"; } } return EXIT_SUCCESS; }

Octave 4.2.2 having ambiguous issue while compiling with c++14
Recently i upgraded to c++14 because of its advanced features, but i am facing ambiguous issue with Octave compilation for Range and Complex variables in my project where i used c++14 to compile ffmpeg,opencv,...etc.
with c++11 i never faced issue with octave 4.0.0 and 4.2.2 but only with c++14 i am facing issue.
It would be great help if someone provide solution to resolve this issue
In file included from /usr/include/octave4.2.2/octave/Array.h:38:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/idxvector.h:173:26: error: reference to ‘Range’ is ambiguous idx_range_rep (const Range&); ^ /usr/include/octave4.2.2/octave/idxvector.h:43:7: note: candidates are: class Range class Range; ^ In file included from /usr/local/include/opencv2/core.hpp:58:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/types.hpp:590:18: note: class cv::Range class CV_EXPORTS Range ^ In file included from /usr/include/octave4.2.2/octave/Array.h:38:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/idxvector.h:206:5: error: reference to ‘Range’ is ambiguous Range unconvert (void) const; ^ /usr/include/octave4.2.2/octave/idxvector.h:43:7: note: candidates are: class Range class Range; ^ In file included from /usr/local/include/opencv2/core.hpp:58:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/types.hpp:590:18: note: class cv::Range class CV_EXPORTS Range ^ In file included from /usr/include/octave4.2.2/octave/Array.h:38:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/idxvector.h:511:21: error: reference to ‘Range’ is ambiguous idx_vector (const Range& r) ^ /usr/include/octave4.2.2/octave/idxvector.h:43:7: note: candidates are: class Range class Range; ^ In file included from /usr/local/include/opencv2/core.hpp:58:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/types.hpp:590:18: note: class cv::Range class CV_EXPORTS Range ^ In file included from /usr/include/octave4.2.2/octave/Array.h:38:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/idxvector.h:1005:35: error: reference to ‘Range’ is ambiguous double& scalar, Range& range, ^ /usr/include/octave4.2.2/octave/idxvector.h:43:7: note: candidates are: class Range class Range; ^ In file included from /usr/local/include/opencv2/core.hpp:58:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/types.hpp:590:18: note: class cv::Range class CV_EXPORTS Range ^ In file included from /usr/include/octave4.2.2/octave/Array.h:38:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/idxvector.h:1005:35: error: ‘Range’ has not been declared double& scalar, Range& range, ^ In file included from /usr/include/octave4.2.2/octave/Array.h:41:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/loutils.h:71:52: error: reference to ‘Complex’ is ambiguous extern OCTAVE_API bool xtoo_large_for_float (const Complex& x); ^ In file included from /usr/include/octave4.2.2/octave/loutils.h:36:0, from /usr/include/octave4.2.2/octave/Array.h:41, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/octcmplx.h:31:30: note: candidates are: typedef struct std::complex<double> Complex typedef std::complex<double> Complex; ^ In file included from /usr/local/include/opencv2/core.hpp:54:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/base.hpp:658:30: note: template<class _Tp> class cv::Complex template<typename _Tp> class Complex; ^ In file included from /usr/include/octave4.2.2/octave/Array.h:41:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/loutils.h:99:24: error: reference to ‘Complex’ is ambiguous template <> OCTAVE_API Complex octave_read_value (std::istream& is); ^ In file included from /usr/include/octave4.2.2/octave/loutils.h:36:0, from /usr/include/octave4.2.2/octave/Array.h:41, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/octcmplx.h:31:30: note: candidates are: typedef struct std::complex<double> Complex typedef std::complex<double> Complex; ^ In file included from /usr/local/include/opencv2/core.hpp:54:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/base.hpp:658:30: note: template<class _Tp> class cv::Complex template<typename _Tp> class Complex; ^ In file included from /usr/include/octave4.2.2/octave/Array.h:41:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/loutils.h:110:8: error: reference to ‘Complex’ is ambiguous inline Complex ^ In file included from /usr/include/octave4.2.2/octave/loutils.h:36:0, from /usr/include/octave4.2.2/octave/Array.h:41, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/octcmplx.h:31:30: note: candidates are: typedef struct std::complex<double> Complex typedef std::complex<double> Complex; ^ In file included from /usr/local/include/opencv2/core.hpp:54:0, from /usr/local/include/opencv2/opencv.hpp:52, from /home/arun/Desktop/proj/media/video.cpp:2: /usr/local/include/opencv2/core/base.hpp:658:30: note: template<class _Tp> class cv::Complex template<typename _Tp> class Complex; ^ In file included from /usr/include/octave4.2.2/octave/Array.h:41:0, from /usr/include/octave4.2.2/octave/Sparse.h:37, from /usr/include/octave4.2.2/octave/MSparse.h:31, from /usr/include/octave4.2.2/octave/MatrixType.h:29, from /usr/include/octave4.2.2/octave/mxbase.h:30, from /usr/include/octave4.2.2/octave/Matrix.h:32, from /usr/include/octave4.2.2/octave/oct.h:30, from /home/arun/Desktop/proj/media/video.h:20, from /home/arun/Desktop/proj/media/video.cpp:7: /usr/include/octave4.2.2/octave/loutils.h:132:47: error: reference to ‘Complex’ is ambiguous octave_write_complex (std::ostream& os, const Complex& cval); ^

Can C++ functions return a pointer to an array of known length?
I have a class that contains a
static constexpr
array ofconst char
s, which i would like to make available via ac_str()
method:class my_class { private: static constexpr const char c_str_[6] = {'c', 'h', 'a', 'r', 's', '\0'}; public: static constexpr const char* c_str() { return c_str_; } };
This works, but has an unfortunate effect: It removes the length of the pointedto array from the type:
decltype(my_class::c_str()) // equivalent to const char*
What I'd really like is some way to achieve this:
decltype(my_class::c_str()) // equivalent to const char[6]
I understand that in either case the returned object will be a pointer; I would just like to preserve the length of the pointedto array in the type. Kind of like how
decltype("string literal")
isconst char[15]
, notconst char*
.Is there a way to do this?

Getting return type of a function pointer in parameter pack and saving it as concatenated tuple with other args
I want to define a tuple type based on passed parameter pack that will have all parameters that are not functions and in place of those that are functions just save their return type. That type will be later used for indexing in hash map so I need a method to acquire it
std::result_of seems like a solution to problem, and in my test code it works if only function pointers are passed. But in my use case one may also pass other basic types and that throws "error: no type named ‘type’ in ‘class std::result_of’" even though i added std::conditional check
template <typename ...Args> using tuple_cat_t = decltype(std::tuple_cat(std::declval<Args>()...)); template <typename ...Args> void eval(Args... args) { using tuple_t = std::tuple<Args...>; using pattern_t = tuple_cat_t< typename std::conditional < std::is_pointer<Args>::value && std::is_function<typename std::remove_pointer<Args>::type>::value, std::tuple < std::result_of_t<Args&&()> > , std::tuple<Args> >::type... >; } int fn(){ return 5; } int main(){ eval(fn,fn); //all good, pattern type is std::tuple<int, int> eval(5,fn); //comilation error }

emplace_back() vs push_back when inserting a pair into std::vector
I defined the following
std::vector<std::pair<int,int> > my_vec; my_vec.push_back( {1,2} ); //this works my_vec.emplace_back( {1,2} ); // this doesn't work std::pair<int,int> temp_pair = {1,2}; my_vec.emplace_back( temp_pair ); //this works
I am compiling with c++11. The third line is problematic, but I thought you can use
emplace_back()
anywhere that you havepush_back()
, but this is apparently wrong. Why does the third line not work? 
Why a vector constructor takes only last value of the parameter when using in unordered_map emplace?
I'm trying to implement unordered_map> with using unordered_map::emplace
#include <iostream> #include <vector> #include <unordered_map> using namespace std; int main () { unordered_map<char,vector<int>> amap; amap.emplace('k',(2,9)); for(auto i : amap['k']){ cout << i; } }
I expected output as "99" because I constructed the vector with (2,9). but actual outcome was "000000000" which the emplace constructed vector as (9), 0 being default and 9 as number of ints. I played around little more with other parameter values and realized the emplace only took last value in vector parameter. Why is it?
I can still accomplish the goal by doing
vector<int> v(2,9); amap.emplace('k',v);
but just wondering why, and save one line of code.

What is the usecase of return value for std::vector::emplace_back in C++17?
I have read in cppreference.com that the new(since C++17)
std::vector::emplace_back
has an return value of referance to the inserted element.Return value
 (none) (until C++17)
 A reference to the inserted element. (since C++17)
I was thinking, while inserting element to the vector, why we need a referance to it? how this could be usful or what is the usecase case of this new return?
Here is a sample code which I wrote to see, the feature.
#include <vector> int main() { std::vector<int> myVec; for(int i = 0; i < 3; ++i) { int& newElement = myVec.emplace_back(i); ^^^^^^^ => why standard should expose the element after inserting. } }