I intended to make a class 'MyVector' has 3D coordinates(X, Y, Z).
I tried making a constructor has three types of function parameter which each parameter type satisfies std::is_arithmetic.
I made two different codes. one is working well, but the other was not working.
Here's my code.
main.cpp
#include <iostream>
#include "MyVector.h"
using namespace std;
int main()
{
MyVector mv1 = MyVector();
int x = 5;
double y = 2.0;
float z = 5.0;
MyVector mv2 = MyVector(z, y, x);
//MyVector mv3 = MyVector(&x);
cout << boolalpha << is_arithmetic<int>::value << endl;
cout << mv2;
}
MyVector.h
#pragma once
#include <type_traits>
#include <iostream>
class MyVector
{
public:
MyVector();
MyVector(const MyVector&);
//This is What I wanted
template <typename N1, typename = std::enable_if_t<std::is_arithmetic<N1>::value >
, typename N2, typename = std::enable_if_t<std::is_arithmetic<N2>::value >
, typename N3, typename = std::enable_if_t<std::is_arithmetic<N3>::value > >
MyVector(const N1& x, const N2& y, const N3& z)
{
X = x;
Y = y;
Z = z;
}
//Working
template <typename N, typename = std::enable_if_t<std::is_arithmetic<N>::value >>
MyVector(const N& x)
{
X = 0;
Y = 0;
Z = 0;
}
//Not Working
template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >>
MyVector(const N& x)
{
X = 0;
Y = 0;
Z = 0;
}
private:
float X;
float Y;
float Z;
public:
friend std::ostream& operator<<(std::ostream&, const MyVector&);
};
I don't know what is difference of two below codes
1. template <typename N, typename = std::enable_if_t<std::is_arithmetic<N>::value >>
2. template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >>