C++ c++;删除括号的宏
假设我的.cpp代码中有类似的东西C++ c++;删除括号的宏,c++,macros,C++,Macros,假设我的.cpp代码中有类似的东西 name(min,max) 我可以写宏吗 #define GET_NAME(A) //transforms name(min,max) -> name; 宏呢 #define GET_RANGE(A) // transforms name(min,max) -> std::pair<T,N>(min,max); #define GET_RANGE(A)//转换名称(min,max)->std::pair(min,max); 用法
name(min,max)
我可以写宏吗
#define GET_NAME(A) //transforms name(min,max) -> name;
宏呢
#define GET_RANGE(A) // transforms name(min,max) -> std::pair<T,N>(min,max);
#define GET_RANGE(A)//转换名称(min,max)->std::pair(min,max);
用法如下
GET_NAME(r(0,255)) -> r
GET_RANGE(r(0,255)) -> std::pair<int,int>(0,255)
#ifndef PIXEL_PIXEL_HPP
#define PIXEL_PIXEL_HPP
#include <ostream>
#include <array>
template<typename T, unsigned int N>
union Pixel;
#define DECLARE_PIXEL(NAME, T, N, ...) \
template<> \
union Pixel<T,N> { \
static const int SIZE = N;\
T args[N]; \
struct{ \
T __VA_ARGS__;\
};\
\
Pixel() = default; \
Pixel(std::initializer_list<T> l) { std::move(l.begin(),l.end(),args); }; \
Pixel(const Pixel<T,N>& other) = default; \
Pixel(Pixel<T,N>&& other) = default; \
T operator[](unsigned int i) const{ \
return args[i];\
} \
T& operator[](unsigned int i) { \
return args[i];\
}\
bool operator==(const Pixel<T,N>& rhs) const{ \
for(int i = 0; i < N; ++i) { \
if(args[i] != rhs[i]) { \
return false; \
} \
} \
return true; \
}; \
bool operator!=(const Pixel<T,N>& rhs) const{ \
return !(*this == rhs); \
}; \
bool operator<(const Pixel<T,N>& rhs) const{ \
for(int i = 0; i < N; ++i) { \
if(args[i] >= rhs[i]) { \
return false;\
}\
} \
return true;\
}; \
bool operator>(const Pixel<T,N>& rhs) const{ \
for(int i = 0; i < N; ++i) { \
if(args[i] <= rhs[i]) { \
return false;\
}\
} \
return true;\
}; \
bool operator<=(const Pixel<T,N>& rhs) const{ \
return !(*this > rhs);\
}; \
bool operator>=(const Pixel<T,N>& rhs) const{ \
return !(*this < rhs);\
}; \
Pixel<T,N>& operator=(const Pixel<T,N>& rhs) = default; \
Pixel<T,N>& operator=(Pixel<T,N>&& rhs) = default; \
Pixel<T,N> operator*(T scalar) const{ \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] * scalar); \
} \
return result;\
}; \
friend Pixel<T,N> operator*(T scalar, const Pixel<T,N>& rhs){ \
return rhs * scalar;\
}\
Pixel<T,N> operator*(const Pixel<T,N>& other) const { \
Pixel<T,N> result;\
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] * other[i]);\
} \
return result;\
};\
Pixel<T,N> operator/(T scalar) const { \
return (*this) * (1/scalar);\
}; \
friend Pixel<T,N> operator/(T scalar, const Pixel<T,N>& pixel) { \
return pixel / scalar;\
}\
Pixel<T,N> operator/(const Pixel<T,N>& other) const { \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] / other[i]);\
} \
return result;\
};\
Pixel<T,N> operator+(const Pixel<T,N>& rhs) const { \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] + rhs[i]); \
} \
return result;\
}; \
Pixel<T,N> operator-(const Pixel<T,N>& rhs) const { \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] - rhs[i]);\
} \
return result; \
} \
Pixel<T,N>& operator +=(const Pixel<T,N>& rhs) { \
*this = *this + rhs; \
return *this;\
} \
Pixel<T,N>& operator -=(const Pixel<T,N>& rhs) { \
*this = *this - rhs; \
return *this;\
} \
Pixel<T,N>& operator *=(const Pixel<T,N>& rhs) { \
*this = *this * rhs; \
return *this;\
} \
Pixel<T,N>& operator /=(const Pixel<T,N>& rhs) { \
*this = *this / rhs; \
return *this;\
}\
friend std::ostream& operator<<(std::ostream& os, const Pixel<T,N>& pixel) { \
for(const auto& elem : pixel.args){ \
os<<elem << " "; \
} \
return os;\
} \
\
}; \
using NAME = Pixel<T,N>; \
DECLARE_PIXEL(RGB, int, 3,r(0,255), g(0,255), b(0,255))
DECLARE_PIXEL(ARGB, float, 4, a(0,1), r(0,1), g(0,1), b(0,1))
#undef DECLARE_PIXEL
#endif //PIXEL_PIXEL_HPP
GET_NAME(r(0255))->r
GET_范围(r(0255))->标准::对(0255)
真正的目标是提供这样的东西
GET_NAME(r(0,255)) -> r
GET_RANGE(r(0,255)) -> std::pair<int,int>(0,255)
#ifndef PIXEL_PIXEL_HPP
#define PIXEL_PIXEL_HPP
#include <ostream>
#include <array>
template<typename T, unsigned int N>
union Pixel;
#define DECLARE_PIXEL(NAME, T, N, ...) \
template<> \
union Pixel<T,N> { \
static const int SIZE = N;\
T args[N]; \
struct{ \
T __VA_ARGS__;\
};\
\
Pixel() = default; \
Pixel(std::initializer_list<T> l) { std::move(l.begin(),l.end(),args); }; \
Pixel(const Pixel<T,N>& other) = default; \
Pixel(Pixel<T,N>&& other) = default; \
T operator[](unsigned int i) const{ \
return args[i];\
} \
T& operator[](unsigned int i) { \
return args[i];\
}\
bool operator==(const Pixel<T,N>& rhs) const{ \
for(int i = 0; i < N; ++i) { \
if(args[i] != rhs[i]) { \
return false; \
} \
} \
return true; \
}; \
bool operator!=(const Pixel<T,N>& rhs) const{ \
return !(*this == rhs); \
}; \
bool operator<(const Pixel<T,N>& rhs) const{ \
for(int i = 0; i < N; ++i) { \
if(args[i] >= rhs[i]) { \
return false;\
}\
} \
return true;\
}; \
bool operator>(const Pixel<T,N>& rhs) const{ \
for(int i = 0; i < N; ++i) { \
if(args[i] <= rhs[i]) { \
return false;\
}\
} \
return true;\
}; \
bool operator<=(const Pixel<T,N>& rhs) const{ \
return !(*this > rhs);\
}; \
bool operator>=(const Pixel<T,N>& rhs) const{ \
return !(*this < rhs);\
}; \
Pixel<T,N>& operator=(const Pixel<T,N>& rhs) = default; \
Pixel<T,N>& operator=(Pixel<T,N>&& rhs) = default; \
Pixel<T,N> operator*(T scalar) const{ \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] * scalar); \
} \
return result;\
}; \
friend Pixel<T,N> operator*(T scalar, const Pixel<T,N>& rhs){ \
return rhs * scalar;\
}\
Pixel<T,N> operator*(const Pixel<T,N>& other) const { \
Pixel<T,N> result;\
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] * other[i]);\
} \
return result;\
};\
Pixel<T,N> operator/(T scalar) const { \
return (*this) * (1/scalar);\
}; \
friend Pixel<T,N> operator/(T scalar, const Pixel<T,N>& pixel) { \
return pixel / scalar;\
}\
Pixel<T,N> operator/(const Pixel<T,N>& other) const { \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] / other[i]);\
} \
return result;\
};\
Pixel<T,N> operator+(const Pixel<T,N>& rhs) const { \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] + rhs[i]); \
} \
return result;\
}; \
Pixel<T,N> operator-(const Pixel<T,N>& rhs) const { \
Pixel<T,N> result; \
for(int i = 0; i < N; ++i) { \
result[i] = static_cast<T>(args[i] - rhs[i]);\
} \
return result; \
} \
Pixel<T,N>& operator +=(const Pixel<T,N>& rhs) { \
*this = *this + rhs; \
return *this;\
} \
Pixel<T,N>& operator -=(const Pixel<T,N>& rhs) { \
*this = *this - rhs; \
return *this;\
} \
Pixel<T,N>& operator *=(const Pixel<T,N>& rhs) { \
*this = *this * rhs; \
return *this;\
} \
Pixel<T,N>& operator /=(const Pixel<T,N>& rhs) { \
*this = *this / rhs; \
return *this;\
}\
friend std::ostream& operator<<(std::ostream& os, const Pixel<T,N>& pixel) { \
for(const auto& elem : pixel.args){ \
os<<elem << " "; \
} \
return os;\
} \
\
}; \
using NAME = Pixel<T,N>; \
DECLARE_PIXEL(RGB, int, 3,r(0,255), g(0,255), b(0,255))
DECLARE_PIXEL(ARGB, float, 4, a(0,1), r(0,1), g(0,1), b(0,1))
#undef DECLARE_PIXEL
#endif //PIXEL_PIXEL_HPP
\ifndef PIXEL\u PIXEL\u水电站
#定义像素\u像素\u HPP
#包括
#包括
模板
联合像素;
#定义像素(名称,T,N,…)\
模板\
联合像素{\
静态常量int SIZE=N\
T参数[N]\
结构{\
T_uuva_uargs_uu\
};\
\
像素()=默认值\
像素(std::initializer_list l){std::move(l.begin(),l.end(),args);}\
像素(常量像素和其他)=默认值\
像素(像素和其他)=默认值\
T运算符[](无符号整数i)常量{\
返回args[i]\
} \
T&运算符[](无符号整数i){\
返回args[i]\
}\
布尔运算符==(常量像素和rhs)常量{\
对于(int i=0;i(常量像素和rhs)常量{\
对于(int i=0;iint main() {
Pixel<rgb> x {{126,127,128}};
Pixel<rgb> y {{129,128,127}};
Pixel<rgb> z = x + y;
Pixel<argb> foo {{1.1f, 2.2f, 3.3f, 4.4f}};
Pixel<argb> bar {{1.1f, 2.2f, 3.3f, 4.4f}};
foo += bar;
std::cout << z << '\n';
std::cout << foo << '\n';
std::cout << sizeof(z) << '\n';
}
{255,255,255}
{2.2,4.4,6.6,8.8}
3