我想知道是否可以使用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的所有值的方法.
我有一个基本问题.我有一个带有数据成员的类:double _mydata[N].(N是模板参数).使用构造函数初始化列表将这些数据初始化为零的语法是什么?是_mydata({0})根据C++标准OK(所以所有的编译器)?
非常感谢你.
如何将默认函数指定为类成员的参数?
从我的代码派生的当前示例是:
#include <iostream>
#include <functional>
template<typename T> struct C
{
static T test(std::function<T(int)> f = [](int i){return i;})
{return f(42);}
};
int main(int argc, char* argv[])
{
C<int>::test(); // ERROR = internal compiler error : in tsubst_copy, at cp/pt.c:11354
C<int>::test([](int i){return i;}); // OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是GCC的错误吗?
用另一种语法可以避免这个问题吗?
你能在其他C++ 11编译器上尝试吗(对于那些有编译器的人来说)?
请考虑以下代码(LWS):
#include <iostream>
#include <chrono>
inline void test(
const std::chrono::high_resolution_clock::time_point& first,
const std::chrono::high_resolution_clock::time_point& second)
{
std::cout << first.time_since_epoch().count() << std::endl;
std::cout << second.time_since_epoch().count() << std::endl;
}
int main(int argc, char* argv[])
{
test(std::chrono::high_resolution_clock::now(),
std::chrono::high_resolution_clock::now());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你必须多次运行它,因为有时,没有明显的区别.但是,当存在的评价的时间之间的可见的差别first和second,结果是下克++以下内容:
1363376239363175
1363376239363174
Run Code Online (Sandbox Code Playgroud)
英特尔和clang下面的以下内容:
1363376267971435
1363376267971436
Run Code Online (Sandbox Code Playgroud)
这意味着在g ++下,second首先评估参数,在intel和clang下first首先评估参数.
根据C++ 11标准,哪一个是真的?
考虑以下C代码:
#include <stdio.h>
int main(int argc, char* argv[])
{
const long double ld = 0.12345678901234567890123456789012345L;
printf("%lu %.36Lf\n", sizeof(ld), ld);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用gcc 4.8.1under 编译Ubuntu x64 13.04,它打印:
16 0.123456789012345678901321800735590983
Run Code Online (Sandbox Code Playgroud)
这告诉我一个长的双重16字节,但小数似乎只对第20位.这怎么可能?16个字节对应四边形,四边形给出33到36个小数.
从数学,算法和元编程递归的角度来看,我有一个具有挑战性的问题.请考虑以下声明:
template<class R1, class R2>
using ratio_power = /* to be defined */;
Run Code Online (Sandbox Code Playgroud)
基于std::ratio类似操作的例子std::ratio_add.给定,两个std::ratio R1和R2此操作应该计算R1^R2if和only if R1^R2是否为有理数.如果它是不合理的,那么实现应该失败,就像当一个人尝试将两个非常大的比率相乘并且编译器说存在整数溢出时.
三个问题:
目前,只有双打可以在用户定义的文字中生成字符模板:
template <char...> double operator "" _x();
// Later
1.3_x; // OK
"1.3"_y; // C++14 does not allow a _y user-
// defined operator to parse that as a template of chars
Run Code Online (Sandbox Code Playgroud)
有没有一种聪明的方法来std::integer_sequence使用用户定义的文字生成字符.换句话说,代码是什么,_y(const char*, std::size_t)以便我最终得到一个std::integer_sequence<char, '1', '.', '3'>?
c++ templates template-meta-programming user-defined-literals c++14
在C++中处理泛型代码时,我会发现一个非常有用的仿std::identity函数(如std::negate).有没有特殊原因导致标准库中没有这个?
此前的答案显示了如何根据呼叫的有效性应用功能:此处.但是,它适用于两个功能.N在C++ 14中,我想知道这个概念是否可以推广到使用智能模板编程技巧的函数.
问题如下:
template <std::size_t N, class... X>
/* [Return type] */ apply_on_validity(X&&... x)
{
// Tricks here
}
// The function would be equivalent to a hypothetical
// template <std::size_t N, class... F, class... Args>
// [Return type] apply_on_validity(F&&... f, Args&&... args)
// where N = sizeof...(F) is the number of functions
Run Code Online (Sandbox Code Playgroud)
在执行方面:
apply_on_validity<3>(f1, f2, f3, a, b, c, d, e);
Run Code Online (Sandbox Code Playgroud)
将:
f1(a, b, c, d, e)如果表达式有效则调用,否则调用f2(a, b, c, d, e)如果表达式有效则调用,否则调用f3(a, …使用函数,可以编写:
template <class T> void f(T&& x) {myfunction(std::forward<T>(x));}
Run Code Online (Sandbox Code Playgroud)
但是有了lambda,我们没有T:
auto f = [](auto&& x){myfunction(std::forward</*?*/>(x));}
Run Code Online (Sandbox Code Playgroud)
如何在lambda中完美转发?是否decltype(x)可以作为类型std::forward?
c++ ×9
c++11 ×5
c++14 ×3
arrays ×2
lambda ×2
templates ×2
algorithm ×1
arguments ×1
c ×1
c++-chrono ×1
constexpr ×1
decltype ×1
function ×1
identity ×1
long-double ×1
math ×1
negate ×1
overloading ×1
precision ×1
std-function ×1