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*)
,但是我敢肯定这应该是合法的。怎么了
最佳答案
您使用的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);
关于c++ - 强制转换为整数或其他类型不能出现在常量表达式中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7651035/