简体   繁体   English

没有编译时间常数

[英]Not compile time constant

I have: 我有:

    static const std::array<std::pair<ServerD, unsigned int>, 4> dataSizes =
{ std::make_pair(ServerD::ContentType, 1)
, std::make_pair(ServerD::RemoteAddress, 2)
, std::make_pair(ServerD::RemoteUser, 3)
, std::make_pair(ServerD::Url, 4)
};

template <unsigned int Index>
struct SizeFinder {
    static const unsigned int SizeFor(ServerD data) {
        return (dataSizes[Index].first == data) ? dataSizes[Index].second :
            SizeFinder<Index - 1>::SizeFor(data);
    }
};

template <>
struct SizeFinder<0> {
    static const unsigned int SizeFor(ServerD data) {
        return (dataSizes[0].first == data) ? dataSizes[0].second :
            0;
    }
};

Why is this not a compile time constant: 为什么这不是编译时间常数:

char tst[SizeFinder<4>::SizeFor(serverD)]

// Error 1 error C2975: 'BufferSize' : invalid template argument for 'isapi::`anonymous-namespace'::GetVariableFor', expected compile-time constant expression //错误1错误C2975:'BufferSize':'isapi ::`anonymous-namespace':: GetVariableFor'的无效模板参数, 预期的编译时常量表达式

I must make this work without constexpr. 我必须在没有constexpr的情况下进行这项工作。 VS2013 still does not have this. VS2013仍然没有这个。

EDIT As it seems static const functions cannot compute at compile time, is there a workaround for C++ 03 ? 编辑似乎静态const函数无法在编译时进行计算,C ++ 03是否有解决方法?

It would appear that you simply need a compile-time mapping from ServerD to unsigned int . 看来您只需要从ServerDunsigned int的编译时映射ServerD Why not embed it in the enumeration values: 为什么不将其嵌入到枚举值中:

enum class ServerD : unsigned int {
    ContentType = 1U,
    RemoteAddress = 2U,
    RemoteUser = 3U,
    Url = 4U
};

char tst[ServerD::Url];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM