我想使输入/输出功能不要多次写格式说明符。 (我不想使用cin / cout函数,因为它们运行缓慢。)
所以,我编写这段代码。
如果有“ int_fast16_t i”和“ string s”,我希望:{scanf(%d,i); cin >> s;} == {in()>> i >> s;}
struct in
{
template<typename typ> in& operator>>(typ& val) { std::cin >> val; return *this; }
in& operator>>(char& val) { std::scanf(" %c", &val); return *this; }
in& operator>>(std::int_fast8_t& val) { std::scanf("%" SCNdFAST8, &val); return *this; }
in& operator>>(std::uint_fast8_t& val) { std::scanf("%" SCNuFAST8, &val); return *this; }
in& operator>>(std::int_fast16_t& val) { std::scanf("%" SCNdFAST16, &val); return *this; }
in& operator>>(std::uint_fast16_t& val) { std::scanf("%" SCNuFAST16, &val); return *this; }
in& operator>>(std::int_fast32_t& val) { std::scanf("%" SCNdFAST32, &val); return *this; }
in& operator>>(std::uint_fast32_t& val) { std::scanf("%" SCNuFAST32, &val); return *this; }
in& operator>>(std::int_fast64_t& val) { std::scanf("%" SCNdFAST64, &val); return *this; }
in& operator>>(std::uint_fast64_t& val) { std::scanf("%" SCNuFAST64, &val); return *this; }
in& operator>>(float& val) { std::scanf("%f", &val); return *this; }
in& operator>>(double& val) { std::scanf("%lf", &val); return *this; }
};
struct out
{
template<typename typ> template<> out& operator<<(typ& val) { std::cout << val; return *this; }
out& operator<<(char& val) { std::printf("%c", val); return *this; }
out& operator<<(std::int_fast8_t& val) { std::printf("%" PRIdFAST8, val); return *this; }
out& operator<<(std::uint_fast8_t& val) { std::printf("%" PRIuFAST8, val); return *this; }
out& operator<<(std::int_fast16_t& val) { std::printf("%" PRIdFAST16, val); return *this; }
out& operator<<(std::uint_fast16_t& val) { std::printf("%" PRIuFAST16, val); return *this; }
out& operator<<(std::int_fast32_t& val) { std::printf("%" PRIdFAST32, val); return *this; }
out& operator<<(std::uint_fast32_t& val) { std::printf("%" PRIuFAST32, val); return *this; }
out& operator<<(std::int_fast64_t& val) { std::printf("%" PRIdFAST64, val); return *this; }
out& operator<<(std::uint_fast64_t& val) { std::printf("%" PRIuFAST64, val); return *this; }
out& operator<<(float& val) { std::printf("%f", val); return *this; }
out& operator<<(double& val) { std::printf("%lf", val); return *this; }
};
但是,它会导致错误。
error: 'in& in::operator>>(int_fast32_t&)' cannot be overloaded with 'in& in::operator>>(int_fast16_t&)'
note: previous declaration 'in& in::operator>>(int_fast16_t&)'
error: 'in& in::operator>>(uint_fast32_t&)' cannot be overloaded with 'in& in::operator>>(uint_fast16_t&)'
note: previous declaration 'in& in::operator>>(uint_fast16_t&)'
error: 'in& in::operator>>(int_fast64_t&)' cannot be overloaded with 'in& in::operator>>(int_fast16_t&)'
note: previous declaration 'in& in::operator>>(int_fast16_t&)'
error: 'in& in::operator>>(uint_fast64_t&)' cannot be overloaded with 'in& in::operator>>(uint_fast16_t&)'
note: previous declaration 'in& in::operator>>(uint_fast16_t&)' ...(other similar error)
int_fast16_t,int_fast32_t和int_fast64_t是否相同?如果是这样,我应该重写什么才能使功能正确?
答案 0 :(得分:4)
问题在于,各种int_fastNN_t
是特定于实现的typedef,它们可能映射到同一事物,因此不能用于重载。
相反,您应该重载基本类型signed char
,short int
,int
,long int
,long long int
和等效的无符号类型。