This question already has an answer here:
List initialization, Initializer_list and related questions in C++
(1 answer)
Closed 3 years ago.
#include <iostream>
#include <array>
template<typename T, std::size_t R, std::size_t C>
class matrix
{
std::array<T, R * C> m_data;
};
int main()
{
matrix<float, 2, 2> a = { 1,2,3,4 }; // COMPILER ERROR!
}
Clang reports that there is no matching constructor!
I have tried writing a constructor of the form
matrix(std::array<T,R*C> a);
and tried experimenting with && as I think the right-hand side of the expression in question is temporary. Which leads me to some confusion. As we would expect that it would be created then assigned(!) to the value of a.
Like others mentioned in the comments, you need a std::initializer_list<T> constructor for your matrix class.
#include <array> // std::array
#include <initializer_list> // std::initializer_list
#include <algorithm> // std::copy
#include <cassert>
template<typename T, std::size_t R, std::size_t C>
class matrix
{
std::array<T, R * C> m_data;
public:
matrix(const std::initializer_list<T> list)
// ^^^^^^^^^^^^^^^^^^^^^^^ --> constructor which takes std::initializer_list
{
assert(R * C == list.size());
std::copy(list.begin(), list.end(), m_data.begin());
}
};
int main()
{
matrix<float, 2, 2> a = { 1,2,3,4 }; // now compiles
}
(See live online)
However, that is not compile-time and come ups with the following drawbacks:
It does not check the passed types are same.
Size of the passed list can not be checked compile-time, as std::initializer_list is/can not be compile time.
Thirdly, it allows narrowing conversion.
In order to disallow the above, one solution is to provide a variadic template constructor which enables the above checks compile-time.
Something like as follows:(See live online)
#include <array> // std::array
#include <initializer_list> // std::initializer_list
#include <type_traits> // std::conjunction, std::is_same
#include <utility> // std::forward
// traits for checking the types (requires C++17)
template <typename T, typename ...Ts>
using are_same_types = std::conjunction<std::is_same<T, Ts>...>;
template<typename T, std::size_t R, std::size_t C>
class matrix
{
std::array<T, R * C> m_data;
public:
template<typename... Ts>
constexpr matrix(Ts&&... elemets) noexcept
{
static_assert(are_same_types<Ts...>::value, "types are not same!");
static_assert(sizeof...(Ts) == R*C, "size of the array does not match!");
m_data = std::array<T, R * C>{std::forward<Ts>(elemets)...};
}
};
int main()
{
matrix<float, 2, 2> a{ 1.f,2.f,3.f,4.f }; // now compiles
// matrix<float, 2, 2> a1{ 1,2,3,4 }; // error: narrowing conversion!
// matrix<float, 2, 2> a1{ 1.f,2.f,3.f, 4 }; // error: types are not same!
// matrix<float, 2, 2> a1{ 1.f,2.f,3.f,4.f, 5.f }; // error: size of the array does not match!
}
You don't need a std::initializer_list<T>.
One of it's drawbacks is that it doesn't check the number of arguments you pass it.
template<typename T, std::size_t R, std::size_t C>
class matrix
{
public:
matrix(const std::initializer_list<T> list) { /*...*/ }
};
int main()
{
matrix<float, 2, 2> a = { 1,2,3,4 }; // compiles
matrix<float, 2, 2> b = { 1,2,3,4,5 }; // also compiles
}
Instead, use the following:
template<typename T, std::size_t R, std::size_t C>
class matrix
{
std::array<T, R*C> m_data;
public:
matrix() = default;
template<typename... Us>
matrix(Us &&...args) : m_data{static_cast<T>(args)...}
{
}
};
int main()
{
matrix<float,2,2> a = {1,2,3}; // ok
matrix<float,2,2> b = {1,2,3,4}; // ok
matrix<float,2,2> c = {1,2,3,4,5}; // error
}
The cast is necessary to avoid a potential narrowing conversion.
Related
I have a class with a member variable std::function customCallback, and I need to let the user customize its behavior. customCallback should take as input a const std::array<int, N>& and a int, and return a std::array<int, N> (where N is a template parameter).
I am currently using std::bind to achieve this, but I can't understand why the compilation fails. What am I missing?
Since the original code involves inheritance and templates, I am including inheritance and templates in my minimal reproducible example (live code here) as well.
The user should be able to use both the constructor OR a member function to set the custom behavior.
base.h:
#include <array>
#include <functional>
template <std::size_t N>
class BaseClass {
public:
virtual std::array<int, N> CustomBehavior(const std::array<int, N>& user_array, int user_number) = 0;
protected:
std::array<int, N> my_array = {0, 0};
};
derived.h:
#include <base.h>
#include <cstddef>
template <std::size_t N>
class DerivedClass : public BaseClass<N> {
public:
DerivedClass() = default;
DerivedClass(std::function<std::array<int, N>(const std::array<int, N>&, int)> custom_f)
: customCallback(std::bind(custom_f, std::ref(std::placeholders::_1), std::placeholders::_2)) {}
void SetCustomBehavior(std::function<std::array<int, N>(const std::array<int>&, int)> custom_f) {
customCallback = std::bind(custom_f, std::ref(std::placeholders::_1), std::placeholders::_2);
}
std::array<int, N> CustomBehavior(const std::array<int, N>& user_array, int user_number) override {
if (customCallback)
this->my_array = customCallback(user_array, user_number);
return this->my_array;
}
private:
std::function<std::array<int, N>(const std::array<int, N>&, int)> customCallback;
};
main.cpp:
#include <derived.h>
#include <cassert>
static constexpr std::size_t MySize = 2;
std::array<int, MySize> my_behavior(const std::array<int, MySize>& input_array, int a) {
return {a * input_array[0], a * input_array[1]};
}
int main() {
std::array<int, MySize> my_array = {1, 1};
// Default constructor (COMPILES)
DerivedClass<MySize> foo_1; // OK
std::array<int, MySize> bar_1 = foo_1.CustomBehavior(my_array, 2);
assert(bar_1[0] == 0 && bar_1[1] == 0);
// Custom constructor (ERROR)
DerivedClass<MySize> foo_2(my_behavior); // COMPILATION ERROR
std::array<int, MySize> bar_2 = foo_2.CustomBehavior(my_array, 2);
assert(bar_2[0] == 2 && bar_2[1] == 2);
// Custom behavior set later on (ERROR)
DerivedClass<MySize> foo_3; // OK
foo_3.SetCustomBehavior(my_behavior); // COMPILATION ERROR
std::array<int, MySize> bar_3 = foo_3.CustomBehavior(my_array, 2);
assert(bar_3[0] == 2 && bar_3[1] == 2);
return 0;
}
I am not including the whole compilation error since it's fairly long, but it can be seen live code here.
Well, for starters, the first error you are getting is an error on std::array<int> in Derived::SetCustomBehavior():
error: wrong number of template arguments (1, should be 2)
You also have an error on std::ref(std::placeholders::_1), it should be just std::placeholders::_1.
Fixing those two mistakes, the code then compiles and runs (using corrected asserts):
Online Demo
That being said, you don't actually need std::bind() at all in this situation. Your custom_f parameter is the same type as your customCallback member, so simply assign custom_f as-is to customCallback.
And do yourself a favor - create type aliases for your std::function and std::array types to make your code more readable.
Try this:
base.h
#include <array>
#include <functional>
#include <cstddef>
template<std::size_t N>
using intArray = std::array<int, N>;
template<std::size_t N>
using callbackType =
std::function<intArray<N>(const intArray<N>&, int)>;
template <std::size_t N>
class BaseClass {
public:
virtual intArray<N> CustomBehavior(const intArray<N>& user_array, int user_number) = 0;
protected:
intArray<N> my_array = {0, 0};
};
derived.h:
#include "base.h"
template <std::size_t N>
class DerivedClass : public BaseClass<N> {
public:
DerivedClass() = default;
DerivedClass(callbackType<N> custom_f)
: customCallback(std::move(custom_f))
{}
void SetCustomBehavior(callbackType<N> custom_f) {
customCallback = std::move(custom_f);
}
intArray<N> CustomBehavior(const intArray<N>& user_array, int user_number) override {
if (customCallback)
this->my_array = customCallback(user_array, user_number);
return this->my_array;
}
private:
callbackType<N> customCallback;
};
main.cpp:
#include "derived.h"
#include <cassert>
static constexpr std::size_t MySize = 2;
using myIntArray = intArray<MySize>;
myIntArray my_behavior(const myIntArray& input_array, int a) {
return {a * input_array[0], a * input_array[1]};
}
int main() {
myIntArray my_array = {1, 1};
// Default constructor
DerivedClass<MySize> foo_1;
myIntArray bar_1 = foo_1.CustomBehavior(my_array, 2);
assert(bar_1[0] == 0 && bar_1[1] == 0);
// Custom constructor
DerivedClass<MySize> foo_2(my_behavior);
myIntArray bar_2 = foo_2.CustomBehavior(my_array, 2);
assert(bar_2[0] == 2 && bar_2[1] == 2);
// Custom behavior set later on
DerivedClass<MySize> foo_3;
foo_3.SetCustomBehavior(my_behavior);
myIntArray bar_3 = foo_3.CustomBehavior(my_array, 2);
assert(bar_3[0] == 2 && bar_3[1] == 2);
return 0;
}
Online Demo
I want to initialize my array items while avoiding unnecessary instances and copies (similar to this question: initialize std::array without copying/moving elements).
An initializer list does work for a small amount of objects.
Bit I want to do this via a code snippet since my array has several hundreds of items...
How can I do this?
#include <array>
#include <iostream>
class mytype {
public:
int a;
mytype() : a(0) {}
mytype(int a) : a(a) {}
};
int main() {
// explict constructor calls to instantiate objects does work
std::array<mytype, 2> a = { { mytype(10), mytype(20) } };
std::cout << a[0].a; // 10
// I want to do something like this - what does not work of course
std::array<mytype, 2> b = { { for (i = 0, i++, i < 2) mtype(10 * i); } };
}
In c++14:
#include <array>
#include <utility>
#include <cstddef>
template <typename T, std::size_t... Is>
std::array<T, sizeof...(Is)> to_array(std::index_sequence<Is...>)
{
return { T(Is*10)... };
}
template <typename T, std::size_t N>
std::array<T, N> to_array()
{
return to_array<T>(std::make_index_sequence<N>{});
}
int main()
{
std::array<mytype, 10> b(to_array<mytype, 10>());
}
DEMO
This is typically accomplished with a pair of templates:
namespace detail {
template<std::size_t... Idx>
auto make_mytype_array(std::index_sequence<Idx...>) {
return std::array<mytype, sizeof...(Idx)>{{
mytype(10 * Idx)...
}};
}
}
template<std::size_t N>
auto make_mytype_array() {
return detail::make_mytype_array(make_index_sequence<N>{});
}
The above are a pair of utility free functions, but can be folded into the class if need be. If you need it for more than just an expression like 10*i, then a lambda can be passed as another argument (templated to be a general "callable"). With copy elision this will all collapse into direct initialization of the result array object.
I'd like to have a class that gets in it's Ctor unlimited parameters of the same type, and stores them into a vector. It should look like that:
class A(int a, int b, **N time parameter of type T**)
: data(**vector will get N times type T**)
{
}
protected:
vector<T> data;
How should I implement it? Solution could be in c++11/14
I got a few errors such as "parameter packs not expanded with ‘…' ", etc..
This code sample might be useful:
#include <vector>
#include <utility>
template<typename T>
class MyClass {
public:
template<typename ...Args>
MyClass(int a, int b, Args&& ...args) :data{ std::forward<Args>(args)... } {}
private:
std::vector<T> data;
};
int main() {
MyClass<char> sample(1, 2, 'a', 'b');
return 0;
}
[EDIT]: Added std::forward, added missing include for utility
Assuming that T could be anything, even something quite large or non-copyable, we'd want to:
preserve efficiency with perfect forwarding.
check types.
std::initializer_list satisfies 2 but not 1.
Simple variadic template expansion satisfies 1 and not 2.
This solution uses variadic template expansion and enable_if to enforce type compatibility.
#include <vector>
#include <utility>
#include <string>
namespace detail
{
constexpr bool all()
{
return true;
}
template<class...Rest>
constexpr bool all(bool b, Rest...rest)
{
return b and all(rest...);
};
}
template<class T>
class A
{
public:
using value_type = T; // say
template<class...Rest,
std::enable_if_t<detail::all(std::is_convertible<Rest, value_type>::value...)>* = nullptr>
A(int a, int b, Rest&&...rest)
: a_(a), b_(b)
{
this->fill(std::forward_as_tuple(std::forward<Rest>(rest)...),
std::make_index_sequence<sizeof...(Rest)>());
}
private:
template<class Tuple, std::size_t...Is>
void fill(Tuple&& t, std::index_sequence<Is...> seq)
{
data_.reserve(seq.size());
using expand = int[];
void(expand{ 0,
(data_.push_back(std::move(std::get<Is>(t))), 0)...
});
}
private:
int a_, b_;
std::vector<value_type> data_;
};
int main()
{
using namespace std::literals;
auto a = A<double>(1, 2, 4.3, 5.5, 6.6);
auto b = A<std::string>(1, 2, "the", "cat"s, "sat on the mat");
// error: no matching constructor...
// auto err = A<std::string>(1, 2, "the", "cat"s, 0.1);
}
Here you go:
#include <iostream>
#include <vector>
template<class T>
struct V
{
V(int n, std::initializer_list<T> l)
: data(l)
{
(void) n;
}
std::vector<T> data;
};
int main()
{
V<int> v(0,{1,2,3});
}
This is not a perfect example since one needs to construct an object with the weird syntax (n, {optional, arguments, of, same, type}) but it does provide wanted behavior.
The following example is similar to fr3nzy90's, but with the coming C++17 it will allow automatic deduction of T from the constructor arguments:
template <class T>
class MyContainer {
private:
std::vector<T> data;
public:
// Take the first T value explicitly so it can be used to deduce
// T from the constructor arguments (C++17 feature).
template <class... Ts>
MyContainer(int a, int b, T const & tval, Ts const &... tvals) :
data{tval, tvals...} {
…
}
// Special case, empty list, no implicit type deduction, because
// there is no T value to deduce it from.
MyContainer(int a, int b) {
…
}
};
For a small software renderer project I want to work on I'd need different types of vectors so I thought I'd template them up.
template<typename T, size_t dim> struct Vector {
std::array<T, dim> data;
Vector(){
data = { 0 };
}
}
This works nice with empty vectors like:
Vector<int, 3> v;
But how can I create a constructor that would accept a sytax like this:
Vector<int, 3> v(1, 2, 3);
Thought an std::initializer_list could work like this:
Vector(std::initializer_list<T> values){
data = values;
}
Vector<int, 3> v({1, 2, 3});
But the compiler says there's no acceptable conversion between std::array and std::initializer_list and the ({1, 2, 3}) syntax looks kinda clunky too.
You can use variadic template:
template <typename ... Ts>
Vector(Ts&&... args) : data{{std::forward<Ts>(args)...}}
{}
With potentially some SFINAE to restrict this constructor to good number of args, and args convertible to T.
It won't work with std::initializer_list, but it will with std::array proxy:
#include <array>
#include <iostream>
template<typename T, size_t dim> struct Vector {
std::array<T, dim> data;
Vector(){
data = { 0 };
}
Vector(std::array<T, dim> initial_values) : data(initial_values) {}
};
int main() {
Vector<int, 3> v({1, 2, 3});
}
In all the modern C++ compilers I've worked with, the following is legal:
std::array<float, 4> a = {1, 2, 3, 4};
I'm trying to make my own class that has similar construction semantics, but I'm running into an annoying problem. Consider the following attempt:
#include <array>
#include <cstddef>
template<std::size_t n>
class float_vec
{
private:
std::array<float, n> underlying_array;
public:
template<typename... Types>
float_vec(Types... args)
: underlying_array{{args...}}
{
}
};
int main()
{
float_vec<4> v = {1, 2, 3, 4}; // error here
}
When using int literals like above, the compiler complains it can't implicitly convert int to float. I think it works in the std::array example, though, because the values given are compile-time constants known to be within the domain of float. Here, on the other hand, the variadic template uses int for the parameter types and the conversion happens within the constructor's initializer list where the values aren't known at compile-time.
I don't want to do an explicit cast in the constructor since that would then allow for all numeric values even if they can't be represented by float.
The only way I can think of to get what I want is to somehow have a variable number of parameters, but of a specific type (in this case, I'd want float). I'm aware of std::initializer_list, but I'd like to be able to enforce the number of parameters at compile time as well.
Any ideas? Is what I want even possible with C++11? Anything new proposed for C++14 that will solve this?
A little trick is to use constructor inheritance. Just make your class derive from another class which has a pack of the parameters you want.
template <class T, std::size_t N, class Seq = repeat_types<N, T>>
struct _array_impl;
template <class T, std::size_t N, class... Seq>
struct _array_impl<T, N, type_sequence<Seq...>>
{
_array_impl(Seq... elements) : _data{elements...} {}
const T& operator[](std::size_t i) const { return _data[i]; }
T _data[N];
};
template <class T, std::size_t N>
struct array : _array_impl<T, N>
{
using _array_impl<T, N>::_array_impl;
};
int main() {
array<float, 4> a {1, 2, 3, 4};
for (int i = 0; i < 4; i++)
std::cout << a[i] << std::endl;
return 0;
}
Here is a sample implementation of the repeat_types utility. This sample uses logarithmic template recursion, which is a little less intuitive to implement than with linear recursion.
template <class... T>
struct type_sequence
{
static constexpr inline std::size_t size() noexcept { return sizeof...(T); }
};
template <class, class>
struct _concatenate_sequences_impl;
template <class... T, class... U>
struct _concatenate_sequences_impl<type_sequence<T...>, type_sequence<U...>>
{ using type = type_sequence<T..., U...>; };
template <class T, class U>
using concatenate_sequences = typename _concatenate_sequences_impl<T, U>::type;
template <std::size_t N, class T>
struct _repeat_sequence_impl
{ using type = concatenate_sequences<
typename _repeat_sequence_impl<N/2, T>::type,
typename _repeat_sequence_impl<N - N/2, T>::type>; };
template <class T>
struct _repeat_sequence_impl<1, T>
{ using type = T; };
template <class... T>
struct _repeat_sequence_impl<0, type_sequence<T...>>
{ using type = type_sequence<>; };
template <std::size_t N, class... T>
using repeat_types = typename _repeat_sequence_impl<N, type_sequence<T...>>::type;
First of what you are seeing is the default aggregate initialization. It has been around since the earliest K&R C. If your type is an aggregate, it supports aggregate initialization already. Also, your example will most likely compile, but the correct way to initialize it is std::array<int, 3> x ={{1, 2, 3}}; (note the double braces).
What has been added in C++11 is the initializer_list construct which requires a bit of compiler magic to be implemented.
So, what you can do now is add constructors and assignment operators that accept a value of std::initializer_list and this will offer the same syntax for your type.
Example:
#include <initializer_list>
struct X {
X(std::initializer_list<int>) {
// do stuff
}
};
int main()
{
X x = {1, 2, 3};
return 0;
}
Why does your current approach not work? Because in C++11 std::initializer_list::size is not a constexpr or part of the initializer_list type. You cannot use it as a template parameter.
A few possible hacks: make your type an aggregate.
#include <array>
template<std::size_t N>
struct X {
std::array<int, N> x;
};
int main()
{
X<3> x = {{{1, 2, 3}}}; // triple braces required
return 0;
}
Provide a make_* function to deduce the number of arguments:
#include <array>
template<std::size_t N>
struct X {
std::array<int, N> x;
};
template<typename... T>
auto make_X(T... args) -> X<sizeof...(T)>
// might want to find the common_type of the argument pack as well
{ return X<sizeof...(T)>{{{args...}}}; }
int main()
{
auto x = make_X(1, 2, 3);
return 0;
}
If you use several braces to initialize the instance, you can leverage list-init of another type to accept these conversions for compile-time constants. Here's a version that uses a raw array, so you only need parens + braces for construction:
#include <array>
#include <cstddef>
template<int... Is> struct seq {};
template<int N, int... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template<int... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template<std::size_t n>
class float_vec
{
private:
std::array<float, n> underlying_array;
template<int... Is>
constexpr float_vec(float const(&arr)[n], seq<Is...>)
: underlying_array{{arr[Is]...}}
{}
public:
constexpr float_vec(float const(&arr)[n])
: float_vec(arr, gen_seq<n>{})
{}
};
int main()
{
float_vec<4> v0 ({1, 2, 3, 4}); // fine
float_vec<4> v1 {{1, 2, 3, 4}}; // fine
float_vec<4> v2 = {{1, 2, 3, 4}}; // fine
}
Explicitly specify that the data type for the initialization to floating point type. You can do this by doing "1.0f" instead of putting "1". If it is a double, put "1.0d". If it is a long, put "1l" and for unsigned long put "1ul" and so on..
I've tested it here: http://coliru.stacked-crooked.com/a/396f5d418cbd3f14
and here: http://ideone.com/ZLiMhg
Your code was fine. You just initialized the class a bit incorrect.
#include <array>
#include <cstddef>
#include <iostream>
template<std::size_t n>
class float_vec
{
private:
std::array<float, n> underlying_array;
public:
template<typename... Types>
float_vec(Types... args)
: underlying_array{{args...}}
{
}
float get(int index) {return underlying_array[index];}
};
int main()
{
float_vec<4> v = {1.5f, 2.0f, 3.0f, 4.5f}; //works fine now..
for (int i = 0; i < 4; ++i)
std::cout<<v.get(i)<<" ";
}