我试图基于一个整数数组创建一个简单的 LookUpTable,其思路是在编译时计算它.
试图使它可以用于我可能拥有的各种整数类型的任何其他未来表,我需要它作为模板.
所以我有一个LookUpTable.h
#ifndef LOOKUPTABLE_H
#define LOOKUPTABLE_H
#include <stdexcept> // out_of_range
template <typename T, std::size_t NUMBER_OF_ELEMENTS>
class LookUpTableIndexed
{
private:
//constexpr static std::size_t NUMBER_OF_ELEMENTS = N;
// LookUpTable
T m_lut[ NUMBER_OF_ELEMENTS ] {}; // ESSENTIAL T Default Constructor for COMPILE-TIME INTERPRETER!
public:
// Construct and Populate the LookUpTable such that;
// INDICES of values are MAPPED to the DATA values stored
constexpr LookUpTableIndexed() : m_lut {}
{
//ctor
}
// Returns the …Run Code Online (Sandbox Code Playgroud) If I have a variadic template along the lines of;
template<typename T>
concept Fooable = requires (T t) { t.bar() -> bool; };
struct Foo {
int big_foo;
template<std::Integral T, Fooable ... U>
explicit Foo(T&& i, U&& ... f) noexcept
: big_foo {std::forward<T>(i)}
{
Something::something(std::forward<U>(f)...);
...
}
};
Run Code Online (Sandbox Code Playgroud)
then the definition of the template and its constraints works as expected.
But if I 'require' more constraints on Foo and so use the 'requires' expression format, such as;
template<typename T, typename …Run Code Online (Sandbox Code Playgroud) 在为项目制作一个小解析器时,我不断收到Segmentation Faults ...跟踪它转换为转置错误.
此代码示例不是原始代码,但会重现故障.
我很惊讶我的编译器都没有发现引用是未初始化的.
GCC和Clang似乎都在没有警告或错误的情况下编译它.
难道他们不应该将v2 的参考标记为未初始化吗?
我对C++比较陌生,正在学习C++ 17/20.但我很好奇为什么,在这种情况下,编译器没有发现v2未定义但无论如何都将引用传递给未定义的对象.
#include <iostream>
#include <vector>
struct A
{
using vectorA = std::vector<A>;
int foo;
vectorA vA;
A() = delete; // Implicit but let's be certain!
A(int f) noexcept
: foo {f},
vA {}
{}
};
A::vectorA& recurse(A::vectorA& head, int tail) noexcept
{head.emplace_back(tail); return head;}
int main()
{
// A tree of A's
A::vectorA v1 {};
// Fill it using recursive fn converting bars into foos,
// a bar at …Run Code Online (Sandbox Code Playgroud)