I'm trying to write a math vector class. A first version goes like :
template <typename T, unsigned int n>
struct Vector {
Vector<T, n>(T t = T()) // default
{
for (int i = 0; i < n; i++)
{
data[i] = t;
}
}
Vector<T, n>(const Vector<T, n> &aVector)
{
for (unsigned int i = 0; i < n; i++)
{
data[i] = aVector.data[i];
}
}
Vector<T, n>(const T arr[n])
{
for (unsigned int i = 0; i < n; i++)
{
data[i] = arr[i];
}
}
T& operator[](unsigned int i);
const T& operator[](unsigned int i) const;
Vector<T, n>& operator=(const Vector<T, n> &aVector);
void normalise();
T data[n];
};
I also have operators (+, *, etc.) declared outside the class, as well as a couple of other classes.
Here's the thing though. For vectors of length 2, 3, 4 I'd like to have a constructor (or a function) that takes x,y (for Vec2), x,y,z or x,y,z,w as parameters.
However, it seems that you can't make specialised constructors for this purpose. How should I proceed in that case ? Do I have to completely specialise three cases ? Wouldn't that imply that I have to rewrite chunks of code ?
I also have a similar Matrix class ( Matrix), and I'm pretty sure I'll need some constructors for rotation, translation, scaling, etc. I'm assuming I'll need to overcome a similar problem.
If you see
I also have operators (+, *, etc.) declared outside the class, as well as a couple of other functions (dot, cross, etc.).
Here's the thing though. For vectors of length 2, 3, 4 I'd like to have a constructor (or a function) that takes x,y (for Vec2), x,y,z or x,y,z,w as parameters.
However, it seems that you can't make specialised constructors for this purpose. How should I proceed in that case ? Do I have to completely specialise three cases ? Wouldn't that imply that I have to rewrite chunks of code ?
I also have a similar Matrix class ( Matrix), and I'm pretty sure I'll need some constructors for rotation, translation, scaling, etc. I'm assuming I'll need to overcome a similar problem.
If you see anything in the code that seems wrong to you, feel free to point it out by the way.
EDIT : In case I was not clear enough, the arrays are meant to be one-dimensional, and all of its components are of the same type. The specialisations are for arrays with 2, 3 and 4 elements.
You may utilize a variadic template:
#include <chrono>
#include <future>
#include <iostream>
#include <stdexcept>
template<typename T, unsigned int n>
struct Vector
{
// Note: We need x and y:
// The default (in the question) is a constructor taking a single argument.
template <typename ... Args>
Vector(T x, T y, Args ... args)
{
static_assert(sizeof ... (Args) == n - 2, "Invalid number of arguments");
auto initializer_list = { x, y, std::forward<Args>(args)... };
std::copy(initializer_list.begin(), initializer_list.end(), data);
}
T data[n];
};
template<typename T, unsigned int n>
void print(const Vector<T, n>& v) {
for(unsigned i = 0; i < n; ++i)
std::cout << v.data[i] << ' ';
std::cout << '\n';
}
int main()
{
Vector<int, 2> v2(1, 2);
Vector<int, 3> v3(1, 2, 3);
Vector<int, 4> v4(1, 2, 3, 4);
print(v2);
print(v3);
print(v4);
// Invalid number of arguments
// Vector<int, 3> e2(1, 2);
// Invalid number of arguments
// Vector<int, 3> e4(1, 2, 3, 4);
return 0;
}
Personally, I would declare different classes for each vector. Vec2<T1, T2>, Vec3<T1, T2, T3>, and Vec4<T1, T2, T3, T4>. Make each Vec* class publicly inherit a un-templated base class Vector, and then make a static function create in Vector with an overload for each Vec* type that will be responsible for creating them. Pseudo-example for Vec3:
template<T1, T2, T3> static std::shared_ptr<Vector> create(T1 a1, T2 a2, T3 a3)
{
return new Vec3<T1, T2, T3>(a1, a2, a3);
}
auto vec = Vector::create<int, int, int>(1, 2, 3);
variadic template and SFINAE may solve that.
but I think the simpler would be to inherit from (specialized) helper
template <typename T, unsigned int n>
struct VectorHelper<T, n>
{
T data[n];
};
template <typename T>
struct VectorHelper<T, 2>
{
VectorHelper(T x, T y) : {data[0] = x; data[1] = y;}
T data[2];
};
template <typename T>
struct VectorHelper<T, 3>
{
VectorHelper(T x, T y, T z) : {data[0] = x; data[1] = y; data[2] = z}
T data[3];
};
template <typename T, unsigned int n>
struct Vector : private VectorHelper<T, n>
{
using VectorHelper<T, n>::VectorHelper; // use base constructor(s)
// your previous implementation without `data` (as member of base)
};
Related
I want to create a function template that creates a list of all legal/valid instances of some class. The class itself is somehow informed about the values that each of its members can take. Function template:
template <typename T>
std::list<T> PossibleInstantiations();
Now, if SomeClass somehow contains information about legal instantiations of all its members (in below example, legal instantiations of i are 1,4,5 and legal instantiations of j are 1.0, 4.5), then
PossibleInstantiations<SomeClass>();
should yield a list containing elements {SomeClass(1,1.0), SomeClass(1,4.5), SomeClass(4,1.0), SomeClass(4,4.5), SomeClass(5,1.0), SomeClass(5,4.5)}.
Of course, adding extra elements (+ associated valid values) should automatically be handled by PossibleInstantiations.
Someclass would be implemented something like below. In what way should the plumbing be added to client classes (e.g. MyClass), and how should PossibleInstantiations be implemented?
class SomeClass
{
public:
int i;
static std::list<int> ValidValuesFori();
double j;
static std::list<double> ValidValuesForj();
SomeClass(int i, double j);
//int k;
//static std::list<int> ValidValuesFork(); //could be implemented at some later stage.
//Which would mean the constructor becomes:
//SomeClass(int i, int j, int k)
//...
//Extra wiring for pointing out that i and ValidValuesFori belong to each other,
//and perhaps for pointing out that i is the first element in the constructor, or so?
//..
};
static std::list<int> SomeClass::ValidValuesFori()
{
return std::list<int>{1, 4, 5};
//Other options:
//std::list<int> ValidValues;
//for (int i = 0; i < 1000; i++)
//{
// if (i % 3 == 0)
// ValidValues.push_back(i);
//}
//return ValidValues;
}
static std::list<double> SomeClass::ValidValuesForj()
{
return std::list<double>{1.0, 4.5};
}
SomeClass::SomeClass(int i, double j)//or other constructor
:i{ i }, j{ j } {}
Why make it hard if you can make it easy? You already say that SomeClass should know which values are allowed for its members. You could make this explicit by moving GetPossibleImplementations() to the class:
class SomeClass
{
public:
static std::vector<SomeClass> GetPossibleImplementations()
{
std::vector<SomeClass> values;
for (int i : ValidValuesFori())
for (double j : ValidValuesForj())
values.push_back(SomeClass(i, j));
return values;
}
// other methods
// [...]
}
Then you can still add the template function, if you need it:
template <typename T>
std::vector<T> GetPossibleImplementations()
{
return T::GetPossibleImplementations();
}
Moving the logic into the class has the following advantages:
Only the class knows which constructor args are valid, so it makes sense to have the logic there.
The cartesian product of valid argument values may not be sufficient for all cases. What if some value of i conflicts with some value of j?
You can still move some logic into helper functions, for example, the cartesian product.
There are situations where you cannot change the implementation of SomeClass and you need an external solution. Even in that case, I think you should keep the logic class-specific: Declare the generic function GetPossibleImplementations<T>() as above, but implement it only for the specific classes:
template <typename T>
std::vector<T> GetPossibleImplementations();
template <>
std::vector<SomeClass> GetPossibleImplementations<SomeClass>()
{
std::vector<SomeClass> values;
for (int i : SomeClass::ValidValuesFori())
for (double j : SomeClass::ValidValuesForj())
values.push_back(SomeClass(i, j));
return values;
}
The main differences between the two versions is that the first version yields a compile error if the template argument T does not support T::GetPossibleImplementations() and the second version yields a link error if GetPossibleImplementations<T> is not implemented.
With Cartesian product, we might do:
// cartesian_product_imp(f, v...) means
// "do `f` for each element of cartesian product of v..."
template<typename F>
void cartesian_product_imp(F f) {
f();
}
template<typename F, typename H, typename... Ts>
void cartesian_product_imp(F f, std::vector<H> const& h,
std::vector<Ts> const&... vs) {
for (H const& he: h) {
cartesian_product_imp([&](Ts const&... ts){
f(he, ts...);
}, vs...);
}
}
template <typename... Ts>
std::vector<std::tuple<Ts...>> cartesian_product(std::vector<Ts> const&... vs) {
std::vector<std::tuple<Ts...>> res;
cartesian_product_imp([&](Ts const&... ts){
res.emplace_back(ts...);
}, vs...);
return res;
}
template <typename T>
std::vector<T> PossibleInstantiations()
{
auto validValuesByArgs = T::ValidValuesForArgs();
auto validArgs =
std::apply([](const auto&... args){ return cartesian_product(args...); },
validValuesByArgs);
std::vector<T> res;
std::transform(validArgs.begin(), validArgs.end(),
std::back_inserter(res),
[](const auto& t){
return std::apply([](const auto&... args) { return T(args...); },
t);
});
return res;
}
With
class SomeClass
{
public:
int i;
double j;
const std::string s;
static std::tuple<std::vector<int>, std::vector<double>, std::vector<std::string>>
ValidValuesForArgs() { return {{1, 4, 5}, {1.1, 4.5}, {"foo", "bar"}}; }
SomeClass(int i, double j, const std::string& s) : i(i), j(j), s(s) {}
};
Demo
I am using the latest version of MSVC with Visual Studio 2015 Community Edition, and I am having some issues with template specialisation and operator overloading.
I have the following (header only) vector class snippet. I have excluded unnecessary code (i.e. identical specialisations for vec2 and vec4).
template <typename T, int n>
struct VectorN
{
T data[n];
T& operator[](const int i);
};
template <typename T, int n>
T& VectorN<T, n>::operator[](const int i)
{
static_assert(i >= 0 && i < n, "Index out of range");
return data[i];
}
template <typename T, int n>
std::ostream& operator<<(std::ostream& os, const VectorN<T, n>& vec)
{
for (auto i = 0; i < n; ++i)
{
os << vec[i];
}
return os;
}
With the following specialisation:
template <typename T>
struct VectorN<T, 3>
{
union
{
T data[3];
struct
{
T x, y, z;
};
};
};
typedef VectorN<int, 3> Vec3i;
typedef VectorN<float, 3> Vec3f;
The main function that I am compiling is:
int main(int argc, char *argv[])
{
Vec3f vec{ 0, 1, 2 };
std::cout << vec << std::endl;
char dump;
std::cin >> dump;
std::cin.clear();
return 0;
}
I would expect that it works, however, I get an Error: C2676 binary '[': 'const VectorN<float,3>' does not define this operator or a conversion to a type acceptable to the predefined operator
I believe that the ostream operator is working as intended, but that the indexing operator is not. Is there anything that I am doing that is obviously wrong, or does the current MSVC simply not support what I am trying to do?
If you want to keep the union-based solution to access a 3-dimensional vector with both data and x, y, z, you can use CTRP to create a base class which is aware of the derived classes' data member:
namespace impl
{
template <typename T, int n, typename TDerived>
struct VectorBase
{
const T& operator[](const int i) const
{
assert(i >= 0 && i < n);
return static_cast<const TDerived&>(*this).data[i];
}
};
}
Your derived classes can then be implemented as follows:
template <typename T, int n>
struct VectorN : impl::VectorBase<T, n, VectorN<T, n>>
{
T data[n];
};
template <typename T>
struct VectorN<T, 3> : impl::VectorBase<T, 3, VectorN<T, 3>>
{
union
{
T data[3];
struct
{
T x, y, z;
};
};
VectorN(T ax, T ay, T az) : data{ax, ay, az} {}
};
A working example is available on wandbox.
The operator<< works because you have an appropriate overloading for your VectorN templated type.
From the other side, operator[] does not work, because there is no operator[] in the specialized version of VectorN (and compiler kindly tell you about it).
You can either add operator[] to the specialized version of VectorN class or, as #DeiDei mentioned, try to move some functionality to the base class.
I'm new to SFINAE and I'm trying to write a simple Vector template. What I'm trying to achieve is to enable the z member variable based on the dimensions set for the Vector.
I have tried to achieve this effect using the following code:
template<unsigned int DIMENSIONS>
class Vector {
// The x and y variables (same as z)
template<typename = typename std::enable_if<DIMENSIONS >= 3>::type>
/// <summary>
/// The z coordinate.
/// </summary>
Float& z;
Vector() : x(values[0]), y(values[1]) {
}
// Other member functions and variables
std::vector<float> values;
};
template <>
Vector<3>::Vector() : x(values[0]), y(values[1]), z(values[2]) {
}
This should enable the z variable when there are 3 or more dimensions. This makes the compiler complain with the following error:
'Vector<DIMENSIONS>::z': only static data member templates are allowed
Also, I'm not entirely sure how to use initialization lists with SFINAE in such a situation. Because if the dimensions are smaller than 3, z would not have to be initialized (since it wouldn't exist). So far I've used a specialized constructor for 3D vectors (see the code above).
But intelliisense still reports that Members 'x', 'y', 'z' are not initialized in this constructor.
Any help would be appreciated.
You could do something like this:
struct no_z_var {};
struct z_var
{
float z;
};
template<std::size_t n>
struct my_vector : std::conditional_t<( n >= 3 ), z_var, no_z_var>
{
float x, y;
};
int main()
{
my_vector<3> mv3;
mv3.z = 3.4f;
my_vector<2> mv2;
mv2.z = 3.4f; // error; no 'z'
}
However, is this a good solution/design? I'm not so sure; it can get messy. It will most likely present other difficulties during the implementation...
You simply can't conditionally enable variables like that. Your best bet is just to provide multiple specializations of Vector:
template <>
struct Vector<2> {
float x, y;
Vector(std::vector<float> const& values)
: x(values[0])
, y(values[1])
{ }
};
template <>
struct Vector<3> {
float x, y, z;
Vector(std::vector<float> const& values)
: x(values[0])
, y(values[1])
, z(values[2])
{ }
};
// etc.
Though it might be more straightforward to use an array instead:
template <size_t DIM>
struct Vector {
std::array<float, DIM> values;
Vector(std::vector<float> const& vs)
: Vector(vs, std::make_index_sequence<DIM>{})
{ }
private:
template <size_t... Is>
Vector(std::vector<float> const& vs, std::index_sequence<Is...> )
: values{{vs[Is]...}}
{ }
};
You could use inheritance.
template<int n>
struct VecBase;
template<>
struct VecBase<1> {
float x;
};
template<>
struct VecBase<2> : VecBase<1> {
float y;
};
template<>
struct VecBase<3> : VecBase<2> {
float z;
};
template<>
struct VecBase<4> : VecBase<3> {
float w;
};
And then define your vector class:
template<int n>
struct Vector : VecBase<n> {
// operations
};
If you want to make n-dimentional operation, you can use std::get and std::index_sequence. Let's start by making overloads for std::get:
namespace std {
template<size_t I, int n, enable_if_t<(I == 0 && I < n), int> = 0>
float& get(VecBase<n>& vec) {
return vec.x;
}
template<size_t I, int n, enable_if_t<(I == 1 && I < n), int> = 0>
float& get(VecBase<n>& vec) {
return vec.y;
}
template<size_t I, int n, enable_if_t<(I == 2 && I < n), int> = 0>
float& get(VecBase<n>& vec) {
return vec.z;
}
template<size_t I, int n, enable_if_t<(I == 3 && I < n), int> = 0>
float& get(VecBase<n>& vec) {
return vec.w;
}
}
Note that you will have to implement them for const.
Then, you can implement your operations by making a base class that implement operations:
template<int, typename>
struct VecOps;
template<int n, std::size_t... S>
struct VecOps<n, std::index_sequence<S...>> : VecBase<n> {
float dotp(Vector<n>& vec) const {
// Where sum is adding each parameter variadically.
return sum((std::get<S>(*this) * std::get<S>(vec))...);
}
};
And finally, make Vector extending VecOps:
template<int n>
struct Vector : VecOps<n, std::make_index_sequence<n>> {};
Note that I don't have a compiler at my disposition for the moment. If your got compiler error, just leave a comment and I'll check this out.
Specialize the entrie class for 1, 2, 3 dimensions if you want this, i.e., template<> class Vector<3> { ... }.
You don't really need SFINAE here. Just use specialization:
template<unsigned int DIMENSIONS>
class Vector {
template<int I>
struct ZContainer {};
template<> struct ZContainer<3> {
float z;
};
ZContainer<DIMENSIONS> possibleZ;
};
This has the advantage that you don't need additional templates. You can just add functions to the ZContainers for your class behavior.
How can I create an Array class in C++11 which can be used like
Array < int, 2, 3, 4> a, b;
Array < char, 3, 4> d;
Array < short, 2> e;
and access it in a way like
a[2][1][2] = 15;
d[1][2] ='a';
I also need to overload operator as
T &operator[size_t i_1][size_t i_2]...[size_t i_D];
which does not exist. How can I do this?
The simplest way to do this is by nesting std::array:
#include<array>
template<class T, size_t size, size_t... sizes>
struct ArrayImpl {
using type = std::array<typename ArrayImpl<T, sizes...>::type, size>;
};
template<class T, size_t size>
struct ArrayImpl<T, size> {
using type = std::array<T, size>;
};
template<class T, size_t... sizes>
using Array = typename ArrayImpl<T, sizes...>::type;
In this solution Array<char, 3, 4> is the same as std::array<std::array<char, 4>, 3> - array consisting of arrays of smaller dimension.
This also shows how you can implement operator[] for many dimensions. operator[] of your object needs to return object for which operator[] is also defined. In this case it is reference to an array of smaller dimension.
Try this:
#include <iostream>
template <typename T, int N1, int... N2>
class Array
{
public:
Array() {}
~Array() {}
Array<T,N2...>& operator[](int index)
{
return data[index];
}
private:
Array<T,N2...> data[N1];
};
template<typename T, int N>
class Array<T,N>
{
public:
Array() {}
~Array() {}
T& operator[](int index)
{
return data[index];
}
private:
T data[N];
};
int main()
{
Array < int, 2, 3, 4> a, b;
Array < char, 3, 4> d;
Array < short, 2> e;
a[0][1][2] = 15;
d[1][2] = 'a';
std::cout << "a[0][1][2] = " << a[0][1][2] << std::endl;
std::cout << "d[1][2] = " << d[1][2] << std::endl;
return 0;
}
You might also want to throw in range checking and perhaps some iterators to be fancy :)
I made an N-dimensional structure with vectors and templates:
//----------------N-dimensional vector--------------------------------
template<int dim,typename T> class n_dim_vector {
public:
typedef std::vector<typename n_dim_vector<dim - 1, T>::vector> vector;
};
template<typename T> class n_dim_vector <0, T> {
public:
typedef T vector;
};
It can be instatiaated with different dimnsion-counts and is prt of a class that represent a search space.
template<int dim, typename T> class n_dim_ssc {
private:
typename n_dim_vector<dim, T>::vector searchspace;
};
My problem: I cannot get operator[] right to access searchspace properly, specifically the return type.
I tried:
template<typename V> std::vector<V>& operator[](unsigned i) {
return searchspace[i];
}
T& operator[](unsigned i) {
return searchspace[i];
}
at first, thinking the compiler would derive typename V as whatever type searchspace contained at all but the last level. Thats what T& operator[](unsigned i) was for.
But alas, doen't work this way. And I cannot work out how it would
EDIT Don't fear, I do not access empty memory, the structure is initialized and filled, I just didn't include the code for clarity's sake.
Also, I don't intend to access it with a single integer, I wanted to use searchspace[i][j]..[k]
The way to let compiler deduces the return type is auto:
In C++14:
auto operator[](unsigned i) { return searchspace[i]; }
In C++11:
auto operator[](unsigned i) -> decltype(searchspace[i]) { return searchspace[i]; }
I'm answering to your comment
Feel free to recommend something better, I'd appreciate it.
The following code shows one way to handle the multidimensional vector at once, i.e. non-recursively. It could be improved in several ways which I didn't consider for now (for instance, I wouldn't want to use and pass that many arrays but rather use variadic parameter lists. This however requires much more and more diffcult code, so I'll let it be.)
#include <numeric>
template<size_t Dim, typename T>
struct MultiDimVector
{
std::array<size_t, Dim> Ndim;
std::array<size_t, Dim> stride;
std::vector<T> container;
MultiDimVector(std::array<size_t, Dim> const& _Ndim) : Ndim(_Ndim), container(size())
{
stride[0] = 1;
for (size_t i = 1; i<Dim; ++i)
{
stride[i] = stride[i - 1] * Ndim[i - 1];
}
}
size_t size() const
{
return std::accumulate(Ndim.begin(), Ndim.end(), 1, std::multiplies<size_t>());
}
size_t get_index(std::array<size_t, Dim> const& indices) const
{
//here one could also use some STL algorithm ...
size_t ret = 0;
for (size_t i = 0; i<Dim; ++i)
{
ret += stride[i] * indices[i];
}
return ret;
}
T const& operator()(std::array<size_t, Dim> const& indices) const
{
return container[get_index(indices)];
}
};
You can use it like
MultiDimVector<3, double> v({ 3, 2, 5 }); //initialize vector of dimension 3x2x5
auto a = v({0,1,0}); //get element 0,1,0
But as I wrote, the curly brackets suck, so I'd rewrite the whole thing using variadic templates.
The problem with your approach is that you're not initializing any memory inside the vector and just trying to return non-existent memory spots. Something on the line of the following (WARNING: uncleaned and unrefactored code ahead):
#include <iostream>
#include <vector>
template<int dim,typename T> class n_dim_vector {
public:
typedef std::vector<typename n_dim_vector<dim - 1, T>::vector> vector;
};
template<typename T> class n_dim_vector <0, T> {
public:
typedef T vector;
};
template<int dim, typename T> class n_dim_ssc {
public:
typename n_dim_vector<dim, T>::vector searchspace;
n_dim_ssc() {}
n_dim_ssc(typename n_dim_vector<dim, T>::vector space) : searchspace(space) {}
n_dim_ssc<dim-1, T> operator[](std::size_t i) {
if(searchspace.size() < ++i)
searchspace.resize(i);
return n_dim_ssc<dim-1, T>(searchspace[--i]);
}
typename n_dim_vector<dim, T>::vector get() {
return searchspace;
}
};
template<typename T> class n_dim_ssc<0,T> {
public:
typename n_dim_vector<0, T>::vector searchspace;
n_dim_ssc() {}
n_dim_ssc(typename n_dim_vector<0, T>::vector space) : searchspace(space) {}
typename n_dim_vector<0, T>::vector get() {
return searchspace;
}
};
int main(int argc, char** argv) {
n_dim_ssc<0, int> ea;
int a = ea.get();
n_dim_ssc<1, int> ea2;
auto dd2 = ea2[0].get();
n_dim_ssc<2, int> ea3;
auto dd3 = ea3[0][0].get();
}
Try it out
will work with an accessor method (you can modify this as you want).
Anyway I strongly have to agree with Kerrek: a contiguous memory space accessed in a multi-dimensional array fashion will both prove to be faster and definitely more maintainable/easier to use and read.