1

I wanted to do a static_assert inside my template on an array created from a parameter pack.

Consider the following code:

template<typename... Args>
auto constexpr CreateArrConst(Args&&... args)
{
  std::array arr
  {
      args...
  };
  return arr;
}

template<typename... Args>
auto constexpr CreateArrConst_NotWorking(Args&&... args)
{
  constexpr std::array arr
  {
      args...
  };
  static_assert(arr.back() == 4);
  return arr;
}

int main() 
{
  static_assert(CreateArrConst(4).back() == 4);
  // uncomment this to reproduce compile error
  // static_assert(CreateArrConst_NotWorking(4).back() == 4);

  return 0;
}

The first template can create a constexpr which I can use at compile time to check inside main. However, once I try to create the same array (this time as constexpr) inside the template in CreateArrConst_NotWorking I get:

error: ‘args#0’ is not a constant expression

Godbolt link to reproduce: https://godbolt.org/z/YjGq76e5j

Does anyone know why I cannot create the constexpr inside the template?

2
  • @StackDanny Did you try to uncomment the 2nd function call in main? Commented Jun 2, 2022 at 7:54
  • args isn't a constant expression, maybe that's why. Commented Jun 2, 2022 at 8:06

1 Answer 1

2

The problem is that even though the template parameter pack named Args is a constant expression the function parameter pack named args isn't.

And since args isn't a constant expression it(args...) cannot be used as an initializer for the constexpr array in your given example.


To solve this you can replace constexpr with const for the array, as then the initializer is not required to be a constant expression.

template<typename... Args>
auto constexpr CreateArrConst_NotWorking(Args... args)
{
//vvvvv------------------->constexpr changed to const
  const std::array arr
  {
      args...
  };
  return arr;
}

Demo

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

9 Comments

True, but then I cannot do the static_assert inside the template!
Agreed, but your question is: "Does anyone know why I cannot create the constexpr inside the template?" for which my answer provides the explanation.
So, if args isn't constexpr, how can I create the constexpr arr in the first template (CreateArrConst)?
@SupAl Please ask a separate question for your follow-up question. I noticed that you edited your orignal question to include your follow up question. Please avoid doing that as it makes the existing answers obsolete. Feel free to ask as many separate questions as you want for your follow up questions. Like you can ask "how can I create the constexpr arr in the first template". Both of your questions are interesting and needs to be separately answered.
makes sense. I'll follow that suggestion.
|

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.