1

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

I must make this work without constexpr. VS2013 still does not have this.

EDIT As it seems static const functions cannot compute at compile time, is there a workaround for C++ 03 ?

4

1 回答 1

1

看起来您只需要一个从ServerD到的编译时映射unsigned int。为什么不将其嵌入枚举值中:

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

char tst[ServerD::Url];
于 2013-11-12T21:52:32.913 回答