我想知道在编译时获取可变参数模板类的第N个参数的最简单和更常见的方法是什么(返回值必须作为编译器的静态const才能进行一些优化).这是我的模板类的形式:
template<unsigned int... T> MyClass
{
// Compile-time function to get the N-th value of the variadic template ?
};
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
编辑:由于MyClass将包含200多个函数,我无法专门化它.但我可以在MyClass中专门化一个结构或函数.
编辑:从经过验证的答案得出的最终解决方案:
#include <iostream>
template<unsigned int... TN> class MyClass
{
// Helper
template<unsigned int index, unsigned int... remPack> struct getVal;
template<unsigned int index, unsigned int In, unsigned int... remPack> struct getVal<index, In,remPack...>
{
static const unsigned int val = getVal<index-1, remPack...>::val;
};
template<unsigned int In, unsigned int...remPack> struct getVal<1,In,remPack...>
{
static const unsigned int val = In; …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,哪个函数可以为外部使用提供最佳优化,为什么?C++ 2011中是否允许"版本4"?
template<unsigned int TDIM> class MyClass
{
public:
static inline unsigned int size() {return _size;} // Version 1
static inline const unsigned int size() {return _size;} // Version 2
static constexpr unsigned int size() {return _size;} // Version 3
static inline constexpr unsigned int size() {return _size;} // Version 4
protected:
static const unsigned int _size = TDIM*3;
};
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
如果我想提取const引用的类型(比如const double&的double),我是否必须使用:
typename std::remove_cv<typename std::remove_reference<Type>::type>::type
Run Code Online (Sandbox Code Playgroud)
要么
typename std::remove_reference<typename std::remove_cv<Type>::type>::type
Run Code Online (Sandbox Code Playgroud)
?
我已经看到英特尔似乎已经包含了一个新的汇编函数来获取从硬件获得的真实随机数.该指令的名称是RdRand,但在互联网上只能看到少量细节:http://en.wikipedia.org/wiki/RdRand
关于这条新指令及其在C++ 11中的使用我的问题如下:
生成的随机数是否RdRand真的随机?(每个位是由不相关的白噪声或量子过程产生的?)
它是Ivy Bridge处理器的一个特殊功能,英特尔是否会继续在下一代CPU中实现此功能?
如何通过C++ 11使用它?也许有,std::random_device但RdRand如果指令可用,编译器是否已经调用?
如何RdRand在编译程序时检查是否真的被调用?
考虑以下课程:
struct C
{
/* Class contents, without any arithmetic operator... */
constexpr operator int() noexcept; // Implicit conversion to int
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:
std::sort当前使用默认<运算符的标准算法?LessThanComparable概念吗?LessThanComparable.在C++标准中,闭包类型定义如下:
[expr.prim.lambda.closure] lambda表达式的类型(也是闭包对象的类型)是一个唯一的,未命名的非联合类类型,称为闭包类型,其属性如下所述.[...]
该标准似乎并未定义未命名的非联合类类型是否为final.将lambdas实现为最终类的编译器是否符合标准,或者我们是否可以保证可以从lambdas继承?
问题不在于从lambdas继承是否有用:它是有用的.问题是标准是否提供此保证.
我想知道如何在没有任何FIND_PACKAGE的情况下查找/链接库.
假设我们有一个名为testlib的"个人"库:
/perso/testlib/include/testlib1.h
/perso/testlib/include/testlib2.h
/perso/testlib/lib/testlib1.a
/perso/testlib/lib/testlib2.a
Run Code Online (Sandbox Code Playgroud)
如何将其与CMake链接?
1)在CMakeLists.txt的代码中直接链接它的功能是什么?
2)如何让用户选择文件的位置?
3)我很难理解CMake解释什么和不解释什么.例如,如果您定义变量$ {MYVARIABLE_INCLUDE_DIR}或$ {MYVARIABLE_LIBRARIES}是"INCLUDE_DIR"或"LIBRARIES"由CMake解释的扩展名,或者如果我调用此变量$ {MYVARIABLE_INCDIR}没有区别?
4)如果在lib目录中有一个包含十个或更多库文件的库,如何执行相同的过程(包括"个人"库)?
5)最后,当你输入时TARGET_LINK_LIBRARIES(myexecutable gmp),你怎么知道这个库的名字是"gmp".为什么不"Gmp"或"GMP"?放入此函数的库的名称是否等于.a文件减去"lib"和".a"?比如libgmp.a - > gmp?如果我想链接一个名为libtestlolexample.a的库,我是否需要输入TARGET_LINK_LIBRARIES(myexecutable testlolexample)?
非常感谢你.
当我考虑以下两个重载时:
template <class... T> void f(const T&... x);
template <class T> void f(const T& x);
Run Code Online (Sandbox Code Playgroud)
我有保证f(x)总是会调用第二个函数,并且永远不会导致歧义.从某种意义上说,无论其类型如何,第二个版本与一个参数的第一个版本相比具有普遍的优先级.
现在考虑一下通用引用和函数的const引用版本的情况:
template <class T> void f(T&& x);
template <class T> void f(const T& x);
Run Code Online (Sandbox Code Playgroud)
我的问题是:它们是这两个函数之间的普遍优先级,不管x的类型(r值引用,引用,cv限定符,指针......),如前一种情况?(如果是的话,优先级是多少?)
以下代码来自cppreference.comstd::lexicographical_compare上的实现示例:
template<class InputIt1, class InputIt2>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2)
{
for ( ; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2 ) {
if (*first1 < *first2) return true;
if (*first2 < *first1) return false;
}
return (first1 == last1) && (first2 != last2);
}
Run Code Online (Sandbox Code Playgroud)
为什么(void)循环中有一个,没有把它放在那里会有什么后果?
我刚刚在这里和这里询问了有关数组和值初始化的两个问题.但是使用这段代码,我迷路了:
#include <iostream>
#include <iomanip>
#include <array>
template <class T, class U = decltype(std::declval<T>().at(0))>
inline U f1(const unsigned int i)
{T x; return x.at(i);}
template <class T, class U = decltype(std::declval<T>().at(0))>
inline U f2(const unsigned int i)
{T x = T(); return x.at(i);}
int main()
{
static const unsigned int n = 10;
static const unsigned int w = 20;
for (unsigned int i = 0; i < n; ++i) {
std::cout<<std::setw(w)<<i;
std::cout<<std::setw(w)<<f1<std::array<int, n>>(i);
std::cout<<std::setw(w)<<f2<std::array<int, n>>(i); …Run Code Online (Sandbox Code Playgroud)