English 中文(简体)
C+++ 使用 std::: vctor 成员变量的 C++ 编译时间类
原标题:C++ Compile-Time Class With std::vector Member variable
最佳回答
Bringing the very helpful comments of user17732522 to light; The problem lies in the fact that std::vector, even though it can be used within a constexpr context, it cannot, itself, be declared as constexpr, nor a class that contains it. (same goes for std::string and anything utilizing dynamic memory allocation) Thus, the toConstant() function has to be called twice, once for getting the vector (not as constexpr) and once for the size of the vector (as constexpr). The class itself is correct, only getBinary() has to change to something like this: consteval auto getBinary() { std::vector vec = toConstant().getVector(); constexpr size_t vecSize = toConstant().getVector().size(); std::array arr; for(size_t i = 0; i < vecSize; ++i) { arr[i] = vec[i]; } return arr; } Thanks again to user17732522, and please, if I explained something wrong, drop a comment.
问题回答
The issue is that for a function to be consteval any memory allocated must be deallocated within that function. It would appear that your compiler does not allocate any memory when declaring a vector, but it clearly then must do so when you call wr.write(1). It seems that memory is not deallocated within toConstant(), perhaps because wr is moved rather than copied or perhaps for another reason. This means toConstant() is not consteval. To make this work you can move the creation of your Writter object into getBinary(). This means the memory will be deallocated within the function in which it was allocated. Note you must also set your vecSize to a compile time constant. My Compiler would not accept the use of std::vector::size() to provide the size for a std::array The following function definition compiled for me. consteval auto getBinary() { Writter wr; wr.write(1); std::vector vec = wr.getVector(); constexpr size_t vecSize = 1; std::array arr; for (size_t i = 0; i < vecSize; ++i) { arr[i] = vec[i]; } return arr; }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签