我想constexpr用一个使用可变参数模板参数生成的模式初始化一个数组.为简单起见,请考虑使用constexpr unsigned类型列表的大小初始化静态数组的问题unsigned, short, char, int, long.我怎么能这样做所有的计算都是在编译期间完成的?我需要解决方案来使用C++类型系统,所以我不能使用宏.
我能想出的最好的结果如下所示,但是使用 并且工作正常.g++ -std=c++11 -Wall -Ofast -S(使用g ++ 4.7)进行编译并检查程序集清楚地表明在运行时将值压入堆栈.有任何想法吗?
如果我能以某种方式告诉扩展n + 1关于扩展n,那么使用如下的数组初始化器将起作用.
static constexpr unsigned foo[] = { compute_element<Args>::value... };
编辑:等等,没关系,我有一个脑力.上面这条线很好......
这是代码答案:
#include <iostream>
template <class... Args>
struct foo
{
static constexpr unsigned bar[] = { sizeof(Args)... };
};
int main()
{
std::cout << foo<unsigned, short, char, int, long>::bar[2] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的宝贵时间!
在C++中,我们可以使用宏或constexpr(如C++ 11所述).我们在C#中可以做些什么?
有关上下文,请参阅"无法声明..."注释:
static class Constant
{
// we must ensure this is compile time const, have to calculate it from ground...
public const int SIZEOF_TEXUTRE_RGBA_U8C4_640x480 = 4 * sizeof(byte) * 640 * 480;
// Cannot declare compile time constant as following in C#
//public const int SIZEOF_TEXUTRE_RGBA_U8C4_640x480_2 = 4 * PixelType._8UC4.PixelSize() * 640 * 480;
}
public static class PixelTypeMethods
{
public static /*constexpr*/ int PixelSize(this PixelType type)
{
int value = (int)type;
int unit_size = value & 0xFF; …Run Code Online (Sandbox Code Playgroud) constexpr const_reference at( size_type pos ) const;
Run Code Online (Sandbox Code Playgroud)
STL容器访问器的这种重载如何与非constexpr参数一起使用?这种过载的经典用例是什么?
我想建立一个可以选择constexpr-ness的课程.当然,我想利用编译时错误检查.
constexpr当给定参数不是常量表达式时,包含的每个函数(构造函数)也必须在运行时工作.这应该是为什么每次你使用的原因static_assert在constexpr功能后,它无法编译函数参数.
这样说,我已经读过,可以使用抛出mechnanism的异常,因为当函数被调用为常量表达式时,可以在编译时评估这些异常.如果可行,那么对于功能问题就解决了.
但是问题仍然没有解决构造函数,因为constexpr构造函数似乎必须没有正文......所以看起来我不能从那里使用异常抛出!
任何的想法?
我想知道在函数内部定义constexpr变量时内部会发生什么.程序是否存储了被调用函数的constexpr变量的每个版本?
例:
template <class T, std::size_t M, std::size_t N>
template <std::size_t M2, std::size_t N2>
Matrix<T, M, N>::Matrix(const Matrix<T, M2, N2>& m)
{
constexpr T m_min(MATHS::min(M, M2));
constexpr T n_min(MATHS::min(N, N2));
std::size_t i(0), j(0);
for ( ; i < m_min ; ++i )
{
for ( j = 0 ; j < n_min ; ++j )
m_elts[i][j] = m.m_elts[i][j];
for ( ; j < N ; ++j )
m_elts[i][j] = MATHS::CST<T>::ZERO;
}
for ( ; i < M ; ++i ) …Run Code Online (Sandbox Code Playgroud) 我想使用全局constexpr变量:
constexpr int foo = 123;
Run Code Online (Sandbox Code Playgroud)
而不是C宏:
#define FOO (123)
Run Code Online (Sandbox Code Playgroud)
在我写的一些代码中.我希望保证相同的行为,从某种意义上说,它不会在运行时占用内存空间,也不会在编译的目标代码中可见/存在(也就是说,它的值将被用作直接的地方相关).
我可以得到这个保证吗?在某些条件下?当然,假设我不是想使用x的地址或任何这样有趣的业务.
以下代码在GCC(4.9.3)和VC++(19.00.23506)中编译良好,但在Clang(3.7.0)中给出了这些错误.
错误:constexpr函数的返回类型'Foo'不是文字类型
注意:'Foo'不是字面值,因为它不是聚合,除了复制或移动构造函数之外没有constexpr构造函数
码:
#include <iostream>
#include <vector>
struct Foo
{
std::vector<int> m_vec;
Foo(const int *foo, std::size_t size=0):m_vec(foo, foo+size)
{;}
//Foo(const std::initializer_list<int> &init):m_vec{init}
//{;}
};
template <std::size_t N>
constexpr Foo make_fooArray(const int (&a)[N]) noexcept
{
return {a,N};
}
int main()
{
Foo f{ make_fooArray({1,2,3}) };
for (auto i : f.m_vec)
std::cout<< i <<" ";
std::cout<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在rextester上运行的代码:
你能否澄清这是一个编译器错误还是我错过了什么?C++ 11标准说什么?
这是另一个案例,它在GCC和VC中编译但在Clang中不编译.
#include <iostream>
template <typename T, std::size_t N>
constexpr std::size_t sizeOf_fooArray(const T (&)[N]) noexcept
{
return N;
}
int …Run Code Online (Sandbox Code Playgroud) 存储在持续时间中的唯一数据是Rep类型的刻度计数.
但是,我注意到了例如:
this_thread::sleep_for采用sleep_durationconst ref.
future::wait_for同样需要durationconst ref.
等等
(1)他们没有通过价值而被转移的任何特殊原因?
(1a)通过const引用传递是否会使一些可能的constexpr优化变得悲观?
这是不正确的还是只是编译器(在我的情况下是g ++ - 7)仍然是错误的?因为它说n没有定义.
template<class T>
auto tup(T const& t)
{
if constexpr(hana::length(t)() % 2)
auto n = hana::append(t, nullptr);
else
auto const& n = t;
return n;
}
int main()
{
std::cout << hana::length(tup(std::tuple(3, "h", 'c'))) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
n 将始终定义,无论编译器将进入哪个分支.
从C ++ 2a开始,虚拟函数现在可以是constexpr。但是据我所知,您仍然不能在constexpr上下文中调用任意函数指针。
动态多态通常使用vtable来实现,其中包含要调用的函数指针。
另外,动态多态性virtual对于调用您不知道在编译时是哪个类型的重写函数很有用。例如:
struct A {
virtual void fn() const {
std::cout << 'A' << std::endl;
}
};
void a_or_b(A const& a) {
// The compiler has no idea `B` exists
// it must be deferred at runtime
a.fn();
}
struct B : A {
void fn() const override {
std::cout << 'A' << std::endl;
}
};
int main() {
// We choose which class is sent
a_or_b(rand() % 2 ? A{} : B{});
}
Run Code Online (Sandbox Code Playgroud)
因此,考虑到那些在编译时无法调用函数指针并且在编译器没有足够的信息来静态地推断要调用的函数时使用了虚拟多态性的方法,虚拟constexpr函数怎么可能?