#include <iostream>
#include <string>
#include <type_traits>
struct FinA{
constexpr FinA(unsigned long long x, int y):x(x), y(y){}
FinA& zero() {
return *this;
}
friend std::ostream&operator << (std::ostream& stm, const FinA& a) {
return stm << "A{" << a.x << a.y << "}";
}
public:
unsigned long long int x;
int y;
};
constexpr FinA operator "" _xy(unsigned long long xy) {
return {xy/1000, int(xy% 1000)};
}
int main() {
// FinA a(1,2);
FinA a {1,2};
// 如果 operator "" _xy 不声明为constexp ,并且在构造的时候需要声明为constexpr
// a2 声明为 constexpr 会报错
constexpr FinA a2 = 12131_xy;
std::cout << a2.x << std::endl;
std::cout << a.y << std::endl;
std::cout << std::boolalpha
<< "object type? " << std::is_object<decltype(a2)>::value << '\n'
<< "scalar type? " << std::is_scalar<decltype(a2)>::value << '\n'
<< "literal type? "
<< std::is_literal_type<decltype(a2)>::value << "\n\n" ;
return 0;
}
c++ constexpr 使用
最新推荐文章于 2023-07-15 09:37:25 发布