Ideone测试用例:http://ideone.com/lzepF
代码:
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <stdint.h>
template <typename T> std::ostream& write(std::ostream& os, T const& x);
template <typename T> std::istream& read(std::istream& is, T& x);
template <typename T, typename U> std::ostream& write(std::ostream& os, std::pair<T, U> const& rh);
template <typename T, typename U> std::istream& read(std::istream& is, std::pair<T, U>& rh);
template <typename T> std::ostream& writeContainer(std::ostream& os, T const& rh);
template <typename T, typename U> std::ostream& write(std::ostream& os, std::map<T, U> const& rh);
template <typename T, typename U> std::istream& read(std::istream& is, std::map<T, U> rh);
template <typename T> std::ostream& write(std::ostream& os, std::vector<T> const& rh);
template <typename T> std::istream& read(std::istream& is, std::vector<T>& rh);
template <typename T>
std::ostream& write(std::ostream& os, T const& x){
static_assert(!std::is_pointer<T>(), "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod<T>(), "That's not a POD: can't write it");
os.write(reinterpret_cast<const char *>(&x), sizeof(T));
return os;
}
template <typename T>
std::istream& read(std::istream& is, T& x){
static_assert(!std::is_pointer<T>(), "That's a pointer, you probably don't want to read that");
static_assert(std::is_pod<T>(), "That's not a POD: can't read it");
is.read(reinterpret_cast<char *>(&x), sizeof(T));
return is;
}
template <typename T, typename U>
std::ostream& write(std::ostream& os, std::pair<T, U> const& rh){
write(os, rh.first);
write(os, rh.second);
return os;
}
template <typename T, typename U>
std::istream& read(std::istream& is, std::pair<T, U>& rh){
read(is, rh.first);
read(is, rh.second);
return is;
}
template <typename T>
std::ostream& writeContainer(std::ostream& os, T const& rh){
uint32_t size = std::distance(rh.begin(), rh.end());
write(os, size);
for(auto it = rh.begin(); it != rh.end(); ++it){
write(os, *it);
}
return os;
}
template <typename T>
std::istream& readContainer(std::istream& is, T& rh){
uint32_t size;
read(is, size);
std::insert_iterator<T> it(rh, rh.end());
for(uint32_t i=0; i<size; ++i) {
typename T::value_type x;
read(is, x);
it = x;
}
return is;
}
template <typename T, typename U>
std::ostream& write(std::ostream& os, std::map<T, U> const& rh){
return writeContainer(os, rh);
}
template <typename T, typename U>
std::istream& read(std::istream& is, std::map<T, U> rh){
return readContainer(is, rh);
}
template <typename T>
std::ostream& write(std::ostream& os, std::vector<T> const& rh){
return writeContainer(os, rh);
}
template <typename T>
std::istream& read(std::istream& is, std::vector<T>& rh){
return readContainer(is, rh);
}
int main(){
{
std::stringstream s;
std::vector<int> x = {0, 1, 2, 3};
write(s, x);
}
{
std::stringstream s;
std::map<int, int> x = {{0, 0}, {1, 2}, {2, 4}, {3, 6}};
write(s, x);
}
return 0;
}
错误:
prog.cpp: In function 'std::ostream& write(std::ostream&, const T&) [with T = unsigned int, std::ostream = std::basic_ostream<char>]':
prog.cpp:57:2: instantiated from 'std::ostream& writeContainer(std::ostream&, const T&) [with T = std::vector<int>, std::ostream = std::basic_ostream<char>]'
prog.cpp:92:30: instantiated from 'std::ostream& write(std::ostream&, const std::vector<T>&) [with T = int, std::ostream = std::basic_ostream<char>]'
prog.cpp:104:13: instantiated from here
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp: In function 'std::ostream& write(std::ostream&, const T&) [with T = int, std::ostream = std::basic_ostream<char>]':
prog.cpp:60:3: instantiated from 'std::ostream& writeContainer(std::ostream&, const T&) [with T = std::vector<int>, std::ostream = std::basic_ostream<char>]'
prog.cpp:92:30: instantiated from 'std::ostream& write(std::ostream&, const std::vector<T>&) [with T = int, std::ostream = std::basic_ostream<char>]'
prog.cpp:104:13: instantiated from here
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
由于我们没有行号,:(,它抱怨FIRST write(s, x)
,在我看来抱怨reinterpret_cast<const char*>(const unsigned int*)
,但我很确定这应该是合法的。
出了什么问题?
答案 0 :(得分:5)
您错误地使用了is_pod
和is_pointer
。您需要将其更改为以下内容:
static_assert(!std::is_pointer<T>::value, "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod<T>::value, "That's not a POD: can't write it");
你可以用这个来测试:
write(std::cout, 1);
int *p = 0;
write(std::cout, p);
答案 1 :(得分:1)
C ++ 03标准5.19“常量表达式”,第1段陈述:
在一些地方,C ++要求表达式计算为整数或枚举常量:作为数组边界(8.3.4,5.3.4),作为case表达式(6.4.2),作为位字段长度(9.6),作为枚举器初始值设定项(7.2),作为静态成员初始值设定项(9.4.2),以及作为整数或枚举非类型模板参数(14.3)。
常量表达式:条件表达式
一个整数常量表达式只能包含文字(2.13),枚举数,常量变量或用常量表达式(8.5)初始化的整数或枚举类型的静态数据成员,整数或枚举类型的非类型模板参数,以及sizeof表达式。浮动文字(2.13.3)只有在转换为整数或枚举类型时才会出现。 只能使用类型转换为整数或枚举类型。特别是,除了sizeof表达式,函数,类对象,指针或引用不得使用,并且赋值,递增,递减,函数 - 不得使用呼叫或逗号运营商。
代码声明:
static_assert(!std :: is_pointer(),“那是一个指针,你可能不想写那个”); static_assert(std :: is_pod(),“那不是POD:不能写它”);
违反此规则,因此编译器会抱怨。