c++ - 转换为整数类型或其他类型不能出现在常量表达式中

标签 c++

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

因为我们没有行号,:( ,它提示第一个 write(s, x) ,在我看来似乎提示 reinterpret_cast<const char*>(const unsigned int*) 但我很确定这应该是合法的。

出了什么问题?

最佳答案

您错误地使用了 is_podis_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/

相关文章:

c++ - 使用控制台和 MSBuild 的 Cpp Core Guidelines Checker

c++ - Makefile 问题 : g++: fatal error: no input files

C++11 变量参数对齐

C++简单的线程问题

c++ - 未找到 NTL 头文件

c++ - 无法从数组 quadraticExpression 中检索数据值

c++ - BOOST_FOREACH 指针 vector

c++ - opencv中的这个语句是什么意思?

c++ - 清除 RWTexture2D 的最有效方法

c++ - 使用 std::variant 作为类成员并应用访问者