1

I have a function that can be called on an enum, save for one value. I want to check statically that the function is not called with that value. I have the following (simplified) (c++11) code:

enum class Channel: char {
    ALL = 0,
    ONE = 1,
    TWO = 2,
};

inline constexpr std::size_t zeroBasedChannelIndex(const Channel channel) {
    static_assert(channel != Channel::ALL, "Channel::All doesn't map to an index");
               // ^^^^^^^ error location
    return static_cast<std::size_t>(channel) - 1;
}

I'm getting the following error though: the value of parameter "channel" (declared at line 16 of "/opt/kvanwel/Workspaces/acdipole/amplib/src/powersoft/kseries/datamodel/../constants/Channel.h") cannot be used as a constant

It looks to me that channel IS constant... but computer says no. What am I doing wrong here?

6
  • 2
    static_assert can't depend on function arguments it doesn't matter if function is a constexpr. static_assert is for time when function is compiled (or when template is instantiated), not when it is executed (even if it is executed during compile time - when compiling other function/code). Commented Dec 12, 2022 at 9:39
  • It doesn't matter if channel is taken as a const. You can call the function with any Channel value. Commented Dec 12, 2022 at 9:41
  • Even when it's an inline constexpr? I thought inline meant that the code at the caller site was replaced with the code inside of the function... Commented Dec 12, 2022 at 9:43
  • 1
    @Typhaon no thats not what inline actually means. Commented Dec 12, 2022 at 9:48
  • stackoverflow.com/a/1759575/4117728 Commented Dec 12, 2022 at 10:04

1 Answer 1

3

The comments above have told you what's wrong. The only thing you can do to get round this is to pass channel as a template parameter:

template <Channel channel> constexpr std::size_t zeroBasedChannelIndex () {
    static_assert(channel != Channel::ALL, "Channel::All doesn't map to an index");
    return static_cast<std::size_t>(channel) - 1;
}

But now, that parameter must be known at compile time (i.e., in practise, a constant, most likely) when calling the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.