I'm wondering whether the tuple can be initialized by initializer list (to be more precise - by initializer_list of initializer_lists)? Considering the tuple definition:
typedef std::tuple< std::array<short, 3>,
std::array<float, 2>,
std::array<unsigned char, 4>,
std::array<unsigned char, 4> > vertex;
is there any way of doing the following:
static vertex const nullvertex = { {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
I just want to achieve same functionality I got using struct instead of tuple (thus only arrays are initialized by initializer_list):
static struct vertex {
std::array<short, 3> m_vertex_coords;
std::array<float, 2> m_texture_coords;
std::array<unsigned char, 4> m_color_1;
std::array<unsigned char, 4> m_color_2;
} const nullvertex = {
{{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}}
};
There is no reason I must use tuples, just wondering. I'm asking, because I'm unable to go through g++ templates errors which are generated by my attempt of such tuple initialization.
#Motti: So I missed the proper syntax for uniform initialization -
static vertex const nullvertex = vertex{ {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
and
static vertex const nullvertex{ {{0, 0, 0}},
{{0.0, 0.0}},
{{0, 0, 0, 0}},
{{0, 0, 0, 0}} };
But it seems that all the trouble lies in arrays, which got no constructor for initializer_list and wrapping arrays with proper constructor seems not so easy task.
Initializer lists aren't relevant for tuples.
I think that you're confusing two different uses of curly braces in C++0x.
initializer_list<T> is a homogeneous collection (all members must be of the same type, so not relevant for std::tuple)
Uniform initialization is where curly brackets are used in order to construct all kinds of objects; arrays, PODs and classes with constructors. Which also has the benefit of solving the most vexing parse)
Here's a simplified version:
std::tuple<int, char> t = { 1, '1' };
// error: converting to 'std::tuple<int, char>' from initializer list would use
// explicit constructor 'std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&)
// [with _U1 = int, _U2 = char, _T1 = int, _T2 = char]'
std::tuple<int, char> t { 1, '1' }; // note no assignment
// OK, but not an initializer list, uniform initialization
The error message says is that you're trying to implicitly call the constructor but it's an explicit constructor so you can't.
Basically what you're trying to do is something like this:
struct A {
explicit A(int) {}
};
A a0 = 3;
// Error: conversion from 'int' to non-scalar type 'A' requested
A a1 = {3};
// Error: converting to 'const A' from initializer list would use
// explicit constructor 'A::A(int)'
A a2(3); // OK C++98 style
A a3{3}; // OK C++0x Uniform initialization
Related
I do not understand why this works fine:
std::array<double, 2> someArray = {0,1};
std::shared_ptr<MyClass> myobj = std::make_shared<MyClass>(someArray);
But this does not work:
std::shared_ptr<MyClass> myobj = std::make_shared<MyClass>({0,1});
Compiler says:
too many arguments to function ‘std::shared_ptr< _Tp> std::make_shared(_Args&& ...)
...
candidate expects 1 argument, 0 provided
Question: Can someone clarify why this happens and if there is any way I can fix the second approach without defining an extra variable?
Edit:
Example of MyClass:
#include <memory> //For std::shared_ptr
#include <array>
#include <iostream>
class MyClass{
public:
MyClass(std::array<double, 2> ){
std::cout << "hi" << std::endl;
};
};
Braced initializers {} can never be deduced to a type (in a template context). A special case is auto, where it is deduced to std::initializer_list. You always have to explictly define the type.
auto myobj = std::make_shared<MyClass>(std::array<double, 2>{0, 1});
The type of {0, 0} is context-dependent. If {0, 0} is being used to immediately construct another object of known type, then it represents a prvalue of that object type:
MyClass m({0, 0});
Here, {0, 0} refers to a prvalue of type std::array<double, 2>
On the other hand, if there are no constraints on the type, then {0, 0} refers to an initializer list of type std::initializer_list<int>:
auto vals = {0, 0};
There's no way to initialize MyClass from std::initializer_list<int>, so make_shared fails to compile:
MyClass m(vals); // Fails: can't construct MyClass from initializer list
How does this connect to std::make_shared? Because std::make_shared is a template, {0, 0} isn't being used to construct a specific type. As a result, it's treated as a std::initializer_list.
I have struct which contains array of inner struct. I want to use method emplace_back() of vector<my_struct>. But I cannot figure how could I initialize this struct correctly:
struct my_struct
{
struct
{
float x, y, z;
} point[3];
};
std::vector<my_struct> v;
v.emplace_back(
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
);
This gives compilation error error: no matching function for call to ‘std::vector<main()::my_struct>::emplace_back(<brace-enclosed initializer list>, <brace-enclosed initializer list>, <brace-enclosed initializer list>)
Is it possible to emplace_back this struct (I'm using C++17)? Should I write custom constructor?
how about this:
v.push_back(my_struct{{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}});
there seems to be a problem with the following code. I get the error message
error: expected unqualified-id before numeric constant
Eigen::Matrix M_inv1_abc = pose_l.block<3, 3>(0,
0).inverse();
This is a code sample:
template<typename T>
Eigen::Matrix<T, 4, 1> Function(Eigen::Matrix<T, 3, 4> pose_l)
{
// fails here
Eigen::Matrix<T, 3, 3> M_inv1 = pose_l.block<3, 3>(0, 0).inverse();
// this works, sample is from https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
Eigen::MatrixXf m(4,4);
Eigen::MatrixXf y(2,2);
m << 1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16;
y = m.block<2,2>(1,1);
}
With the sample MatrixXf I don't use my template...
I renamed pose_l and M_inv1; in other posts, like
Expected unqualified-id before numeric constant for defining a number
a redefinition helped, but not in my case.
What am I missing?
Best
ManuKlause
pose_l.template block<3, 3>(0, 0).inverse();
For detail, you can refer to how c++ deduce the type of variables
Try this (adding parentheses around the block sub-expression):
template<typename T>
Eigen::Matrix<T, 4, 1> Function(Eigen::Matrix<T, 3, 4> pose_l)
{
Eigen::Matrix<T, 3, 3> M_inv1 = (pose_l.block<3, 3>(0, 0)).inverse();
// ...
}
Since C++11, it is possible to initialize member variables in class definitions:
class Foo {
int i = 3;
}
I know I can initialize an std::array like this:
std::array<float, 3> phis = {1, 2, 3};
How can I do this in a class definition? The following code gives an error:
class Foo {
std::array<float, 3> phis = {1, 2, 3};
}
GCC 4.9.1:
error: array must be initialized with a brace-enclosed initializer
std::array<float, 3> phis = {1, 2, 3};
^ error: too many initializers for 'std::array<float, 3ul>'
You need one more set of braces, which is non-intuitive.
std::array<float, 3> phis = {{1, 2, 3}};
I have defined a POD type as below:
template<typename kernelEntryT, size_t kernelRowSize, size_t kernelColSize>
class ImageProcessing::Kernel {
kernelEntryT kernelMatrix[kernelRowSize][kernelColSize];
};
int main(){
ImageProcessing::Kernel<int,3,3> k = {{0,0,0},{0,1,0},{0,0,0}};
}
It does not compile, and tell me:
error: could not convert ‘{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}’ from ‘<brace-enclosed initializer list>’ to ‘ImageProcessing::Kernel<int, 3ul, 3ul>’
ImageProcessing::Kernel<int,3,3> k = {{0,0,0},{0,1,0},{0,0,0}};
Edit: Test Code
You have a missing set of braces (the data member is a single array) and you need to make the data member public, because an aggregate cannot have private or protected members.
This is a simplified, working example:
#include <cstddef> // for std::size_t
template<typename T, std::size_t N, std::size_t M>
class Kernel {
public:
T kernelMatrix[N][M];
};
int main(){
Kernel<int,3,3> k = { {{0,0,0}, {0,1,0}, {0,0,0}} };
}