Ideone caso_prueba: http://ideone.com/lzepFmoldeada de tipo integral o de otro tipo no puede aparecer en una expresión constante
Código:
#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;
}
Errores:
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
ya que no tenemos los números de línea,: (, se queja sobre el PRIMERO write(s, x)
, y me parece que me quejo del reinterpret_cast<const char*>(const unsigned int*)
pero estoy bastante seguro de que debería ser legal.
¿Qué está pasando?
En lugar de 'std :: is_pointer()' etc., ¿no debería ser 'std :: is_pointer :: value'? –