Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2332
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T22:08:19+00:00 2022-05-31T22:08:19+00:00

c++ – Bug in constexpr function MSVC E0028 “attempt to access expired storage”?

  • 0

[ad_1]

I have some constexpr code that compiles and executes with MSVC, CLANG, and GCC. However, in Visual Studio 2022 latest, it places red squiggles under the function with the following (intellisense?) error listed:

“E0028 expression must have a constant value” This is followed by “attempt to access expired storage” followed by 6 lines that all are: “called from”

The problem is associated with the add() method when using the vector::insert(). It goes away if I use an alternate algorithm that doesn’t use insert.

Here’s what the squiggles look like:

Showing red squiggles

What is causing this? The code this subset is from compiles cleanly

Here’s the full code:

#include <algorithm>
#include <vector>
#include <initializer_list>
#include <array>

template<class T>
class A
{
public:
    std::vector<T> v;
    A() = default;
    constexpr A(std::initializer_list<T> list)
    {
        for (auto& p1 : list)
            v.push_back(p1);
    }

    constexpr A add(const A& new_rows) const
    {
#if 0
        // this works
        A ret; ret.v.resize(v.size() + new_rows.v.size());
        for (size_t i = 0; i < ret.v.size(); i++)
            if (i < v.size())
                ret.v[i] = v[i];
            else
                ret.v[i] = new_rows.v[i-v.size()];
#else
        // Bug shows up in "else" intellisense errors: red underline of foo()
        A ret = *this;
        ret.v.insert(ret.v.end(), new_rows.v.begin(), new_rows.v.end());
#endif
        return ret;
    }
};

// return a std::array made from A
template <typename T, size_t a_rows>
constexpr auto make_std_array(const A<T>& mat)
{
    std::array<T, a_rows> ret{};
    for (size_t r = 0; r < a_rows; r++)
        ret[r] = mat.v[r];
    return ret;
}

constexpr auto foo()
{
    A<int> z1{ 1,2,3,4 };
    z1 = z1.add({5,6});
    auto array = make_std_array<int,6>(z1);
    return array;
}

constexpr auto z = foo();
int main()
{
    constexpr auto x = foo();
    return x[5];  // returns 6
}

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 3 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 5 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 4 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.