我想知道是否可以使用constexpr函数初始化整个数组(使用C++ 2011).在这里,我有一些东西来说明我想做的事情:
template<unsigned int DIM> const unsigned int MyClass<DIM>::_myVar[2][3] = {
{metaFunction(0, 0, DIM), metaFunction(0, 1, DIM), metaFunction(0, 2, DIM)},
{metaFunction(1, 0, DIM), metaFunction(1, 1, DIM), metaFunction(1, 2, DIM)}
};
template<unsigned int DIM> inline constexpr unsigned int MyClass<DIM>::metaFunction(const unsigned int k, const unsigned int n, const unsigned int dim)
{
return (((n < dim) && (k < n)) ? (1<<(n-k)) : (0));
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用constexpr初始化myVar而无需手动填充数组.如果存在,给定示例的语法是什么?
为了精确地解决这个问题,我搜索了一种使用单个函数调用来填充myVar的所有值的方法.
在标准论文P0092R1中,Howard Hinnant写道:
template <class To, class Rep, class Period,
class = enable_if_t<detail::is_duration<To>{}>>
constexpr
To floor(const duration<Rep, Period>& d)
{
To t = duration_cast<To>(d);
if (t > d)
--t;
return t;
}
Run Code Online (Sandbox Code Playgroud)
这段代码怎么样?问题是,operator--a std::chrono::duration不是constexpr操作.它被定义为:
duration& operator--();
Run Code Online (Sandbox Code Playgroud)
然而,这段代码编译,并在编译时给出正确的答案:
static_assert(floor<hours>(minutes{3}).count() == 0, "”);
Run Code Online (Sandbox Code Playgroud)
那是怎么回事?
我想知道是否可以在编译时自动构造/初始化static const Eigen::Matrix ?我想这强制需要所有 Eigen::Matrix 类型的constexpr CTOR 等。但是,我在 Eigen3 doxy 中发现没有有关任何constexpr方法的信息。
有人可以引用或回答这些陈述/问题吗?
cppreference指出:
在对象声明或非静态成员函数中使用的constexpr说明符(直到C++ 14)暗示const.
"对象声明"是否意味着"任何变量声明"?
即
constexpr const int someConstant = 3;
Run Code Online (Sandbox Code Playgroud)
相当于
constexpr int someConstant = 3;
Run Code Online (Sandbox Code Playgroud)
在C++ 11,C++ 14和C++ 17中?
考虑以下两个程序:
#include<variant>
#include<iostream>
constexpr auto f() {
using T = std::variant<bool, int>;
T t(false);
t = T(true);
return std::get<bool>(t);
}
template<auto V>
void print() { std::cout << V << "\n"; }
int main() {
print<f()>();
}
Run Code Online (Sandbox Code Playgroud)
和
#include<variant>
#include<iostream>
constexpr auto f() {
using T = std::variant<bool, int>;
T t(false);
t = T(42);
return std::get<int>(t);
}
template<auto V>
void print() { std::cout << V << "\n"; }
int main() {
print<f()>();
}
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会汇编这两项并输出预期结果.在两种情况下,Clang都不会使用以下错误消息编译它们中的任何一个:
<source>:4:16: error: constexpr function never produces a …Run Code Online (Sandbox Code Playgroud) 我刚开始做概念。检查表达式的语法非常有用,它为我删除了很多 sfinae 的样板文件。但是我想知道如何检查表达式是否可以在 constexpr 上下文中使用。有时这些表达式会导致无效。我可以想象的方式看起来像这样,没有注释 constexpr 关键字:
template<typename T>
concept foo = requires(T t) {
/* constexpr */ { t.bar(); } -> std::same_as<void>;
/* constepxr */ { T::foo; };
}
Run Code Online (Sandbox Code Playgroud)
但是,我非常怀疑这是正确的语法。有没有一种简单的方法来检查概念中表达式的constexpr-ness?
我不想检查 constexpr 函数的计算对于 的所有可能值是否不会失败t,我想知道我是否可以将该表达式放在编译器期望某些东西在编译时可评估的地方。
考虑这个代码片段(Godbolt):
#include <cstdio>
#include <string>
#include <string_view>
struct Option
{
std::string_view name;
constexpr Option( std::string_view const n ) noexcept : name{n} {}
};
template< std::size_t N >
class TransformedOption : public Option
{
public:
constexpr TransformedOption( std::string_view const nameStr ) :
Option{ { nameStorage_, N - 1 } }
{
for ( auto i = 0U; i < N; ++i )
{
if ( nameStr[ i ] == '_' ) { nameStorage_[ i ] = '-'; }
else …Run Code Online (Sandbox Code Playgroud) 这有效:(A)
class Foo {
public:
const bool b;
constexpr ~Foo() = default;
constexpr Foo(const bool b) : b(b) {};
};
class Bar {
public:
static constexpr Foo tru { true };//Foo is complete type
};
Run Code Online (Sandbox Code Playgroud)
编译失败:(B)
class Bar {
public:
class Foo {
public:
const bool b;
constexpr ~Foo() = default;
constexpr Foo(const bool b) : b(b) {};
};
static constexpr Foo tru { true };//undefined constructor 'Foo' cannot be used
};
Run Code Online (Sandbox Code Playgroud)
错误:
$ clang++ --std=c++20 -D_POSIX_C_SOURCE=200112L -fPIC -g -Werror …Run Code Online (Sandbox Code Playgroud) 以下 constexpr 函数无法编译:
constexpr void fnc()
{
constexpr int i = 5;
constexpr auto ptr = &i;
}
Run Code Online (Sandbox Code Playgroud)
ptr考虑到所有计算都发生在 constexpr 函数中并且无需声明 constexpr 即可工作,为什么不能是ptrconstexpr?
以下代码只能在 GCC 上编译(在 godbolt.org 上检查了 10.4 和 13.2),但不能在 Clang 上编译(在我尝试过的所有版本上都失败,例如 godbolt.org 上的 17.0.1):
struct A {
static constexpr int b{1};
};
int main(int argc, char *argv[]) {
A a;
A& aref{a};
constexpr auto bb1{a.b};
constexpr auto bb2{aref.b};
return bb1+bb2;
}
Run Code Online (Sandbox Code Playgroud)
叮当输出:
struct A {
static constexpr int b{1};
};
int main(int argc, char *argv[]) {
A a;
A& aref{a};
constexpr auto bb1{a.b};
constexpr auto bb2{aref.b};
return bb1+bb2;
}
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/nG4j3KefE
为什么?
c++ ×10
constexpr ×10
c++20 ×3
c++11 ×2
c++17 ×2
arrays ×1
c++-chrono ×1
c++-concepts ×1
c++14 ×1
clang ×1
clang++ ×1
eigen3 ×1
equivalent ×1
kotlin ×1
variant ×1