在这样的代码中:
#include <iostream>
template<int I>
struct A {
static constexpr int I1 = I + 1;
static constexpr int I2 = I1 + 1;
};
int main() {
std::cout << A<1>::I1 << " " << A<1>::I2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
是否可以安全地假设I2将正确初始化,即I1之前已初始化I2?
我目前正在尝试找到一种干净的方法来创建模板化的字节数组结构,该结构可以根据其构造函数是否使用或不使用 constexpr 关键字实例化来进行不同的初始化,并且如果可能的话,我还想将其应用于其他方法和函数,我想知道是否有办法实现这一点?
这是我想要实现的目标的示例:
#include <iostream>
#include <cstring>
template<size_t N>
struct bytearray {
char data[N];
constexpr bytearray(char val) : data() {
if constexpr (... is compile time instantiated ...) {
for (size_t i = 0; i < N; ++i) {
data[i] = val;
}
} else {
std::memset(data, val, N);
}
}
};
int main() {
constexpr bytearray<10> A(7);
bytearray<10> B(7);
}
Run Code Online (Sandbox Code Playgroud)
因此,A在编译时实例化该类将使用for循环,memcpy如果不是的话。
c++ templates template-meta-programming constexpr compile-time-type-checking
struct Foo
{
struct Bar
{
int data = 0;
//constexpr Bar() = default; // Doesn't work either
constexpr Bar() : data(0) {}
};
static constexpr Bar bar = {}; // ERROR
//static constexpr Bar bar = {0}; // Works if the ctor from Bar is removed
};
Run Code Online (Sandbox Code Playgroud)
Clang 和 GCC(使用 std=c++20)说我尝试使用的构造函数未定义。但如果我改为 ,它就会constexpr起作用inline。我想了解使用“contexpr”有什么问题。
我尝试编写一个函数,它使用C++ 0x constexpr返回一个只有输入集最高位的整数.
constexpr inline uint64_t
get_highest_bit(uint64_t p)
{
return
(p|=(p>>1)),
(p|=(p>>2)),
(p|=(p>>4)),
(p|=(p>>8)),
(p|=(p>>16)),
(p|=(p>>32)),
(p-(p>>1));
}
Run Code Online (Sandbox Code Playgroud)
这使用gcc 4.6.1导致编译时失败.
error: expression ‘(p <unknown operator> ((p >> 1) | p))’ is not a constant-expression
Run Code Online (Sandbox Code Playgroud)
请注意,它没有constexpr关键字.
我的问题是:
为什么这不起作用?我可以看到运算符| =不是constexpr,但对于内置类型是否重要?
有没有一种简单的方法可以将此函数写为constexpr?我希望它在运行时合理有效,我关心可读性.
我正在尝试在编译时初始化一些C++数组但是我得到了一个奇怪的g ++错误.这是我能够获得的最小代码块,它可以重现错误:
#include <array>
template<typename Ar, int... Vals>
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int))
{ return {{ fun(Vals)... }}; }
constexpr int add(int i) { return i + 1; }
constexpr auto b = Map<std::array<int, 2>, 1, 2>(add);
Run Code Online (Sandbox Code Playgroud)
编译器在抱怨
bug.cpp:8:53: in constexpr expansion of ‘Map<std::array<int, 2ul>, {1, 2}>(add)’
bug.cpp:4:80: error: expression ‘add’ does not designate a constexpr function
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int)) { return {{ fun(Vals)... }}; }
Run Code Online (Sandbox Code Playgroud)
这与g ++ 4.7.1和4.9.0 20130520(实验)一起发生.需要注意的是,如果我取代typename Ar::value_type通过int(见注释)中的定义
Map …
使用C++ 14.为什么这会编译:
template<unsigned N>
constexpr bool foo()
{
std::array<char, N> arr;
return true;
}
Run Code Online (Sandbox Code Playgroud)
但不是吗?
constexpr bool foo()
{
std::array<char, 10> arr; // Non-constexpr constructor 'array' cannot be used in a constant expression
return true;
}
Run Code Online (Sandbox Code Playgroud) 何时使用constexpr以及何时使用extern const?
我有这样的情况:
在标题(.h)中:
extern const int MAX_NUMBER_OF_ROWS;
Run Code Online (Sandbox Code Playgroud)在源(.cpp):
const int MAX_NUMBER_OF_ROWS= 99;
Run Code Online (Sandbox Code Playgroud)文件(标题和源)仅包含此类定义和声明.
是否建议只使用constexpr头文件并删除源文件,就像在这里一样?:
// this is in the header file. There is no cpp file any more.
constexpr int MAX_NUMBER_OF_ROWS= 99;
Run Code Online (Sandbox Code Playgroud) // since we can dedfine a static array like this
int a[5] = {0};
// decltype(a[5]) is `int [5]`
// Then how about this way?
constexpr int sum(int a, int b) { return a + b; }
int a, b;
std::cin >> a >> b;
int arr[sum(a, b)] = {0};
Run Code Online (Sandbox Code Playgroud)
它可以成功编译,但是是arr一个静态数组?当我尝试arr用typeid().name()或打印类型时boost::typeindex::type_id_with_cvr,我得到以下错误:
error: cannot create type information for type 'int [(<anonymous> + 1)]' because it involves types of variable size
std::cout << typeid(decltype(arr)).name() …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
constexpr int log2(const unsigned int x) {
return x < 4 ? 1 : 1 + log2(x / 2);
}
int main() {
bitset<log2(2)> foo;
int bar[log2(8)];
cout << log2(8) << endl;
}
Run Code Online (Sandbox Code Playgroud)
这在gcc中可以正常工作:https://ideone.com/KooxoS
但是当我尝试使用visual-studio-2017时,出现以下错误:
错误C2975::预期的编译时常量表达式的
_Bits无效模板参数 注:请参见 错误C2131的声明:表达式未求值为常量 注:失败是由未定义函数的调用或未声明的 注引起的:请参见std::bitset_Bitsconstexprlog2
显然log2是constexpr这样,所以我认为这只是visual-studio-2017中的错误。有办法解决这个错误吗?
c++ compile-time-constant constexpr c++11 visual-studio-2017
我有以下示例代码
template<class T1, class T2>
class Operation
{
public:
constexpr Operation(const T1& lhs, const T2& rhs) noexcept
: m_lhs(lhs), m_rhs(rhs) { }
private:
const T1& m_lhs;
const T2& m_rhs;
};
int main()
{
constexpr int a = 3;
constexpr int b = 4;
constexpr Operation op(a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用cygwin(gcc 8.2)编译
template<class T1, class T2>
class Operation
{
public:
constexpr Operation(const T1& lhs, const T2& rhs) noexcept
: m_lhs(lhs), m_rhs(rhs) { }
private:
const T1& m_lhs;
const T2& m_rhs; …Run Code Online (Sandbox Code Playgroud)