我最近发布了一个关于由于C++中的虚拟性导致的内存开销的问题.答案让我了解vtable和vptr的工作原理.我的问题如下:我在超级计算机上工作,我有数十亿个对象,因此我不得不关心由于虚拟性造成的内存开销.经过一些测量,当我使用具有虚函数的类时,每个派生对象都有其8字节的vptr.这根本不可忽视.
我想知道intel icpc或g ++是否有一些配置/选项/参数,使用"全局"vtable和可调精度的索引而不是vptr.因为这样的事情将允许我使用2字节索引(unsigned short int)而不是8字节vptr用于数十亿个对象(并且很好地减少了内存开销).有没有办法用编译选项做到这一点(或类似的东西)?
非常感谢你.
我已经阅读了关于C++ 11中移动语义的一些描述,我想知道它可以在什么上下文中使用.目前,许多C++数学库使用模板元编程来延迟评估.
如果M = A + B + C*D,其中M,A,B,C和D是矩阵,则模板元编程允许避免无用的副本.移动语义是一种更方便的方式来做这些事吗?
如果不是,则在什么上下文中使用移动语义.如果是,与模板元编程相比,这种用途有什么区别/限制?
C++ 2011标准库的nextafter和nexttoward函数有什么区别?
以下函数的最通用语法是什么:
template<IteratorType> void myFunction(const IteratorType& myIterator)
{
_ptr = &myIterator[0];
}
Run Code Online (Sandbox Code Playgroud)
它需要一个迭代器myIterator(它可以是一个原始指针),目标是将指向的对象的地址分配myIterator给一个原始指针_ptr.目前我使用&myIterator[0]但我意识到只有随机访问迭代器才有operator [].
那么有一种语法适用于所有类型的标准迭代器和指针吗?
考虑以下功能:
// Declaration in the .h file
class MyClass
{
template <class T> void function(T&& x) const;
};
// Definition in the .cpp file
template <class T> void MyClass::function(T&& x) const;
Run Code Online (Sandbox Code Playgroud)
noexcept如果类型T不是可构造的,我想创建此函数.
怎么做 ?(我的意思是什么语法?)
请考虑以下代码:
template <class>
struct test: std::integral_constant<int, 0> {};
template<class R, class C, class... Args>
struct test<R(C::*)(Args...)>: std::integral_constant<int, 1> {};
template<class R, class C, class... Args>
struct test<R(*C::*)(Args...)>: std::integral_constant<int, 2> {};
template<class R, class C, class... Args>
struct test<R(**C::*)(Args...)>: std::integral_constant<int, 3> {};
template<class R, class C, class... Args>
struct test<R(C::**)(Args...)>: std::integral_constant<int, 4> {};
template<class R, class C, class... Args>
struct test<R(C::***)(Args...)>: std::integral_constant<int, 5> {};
Run Code Online (Sandbox Code Playgroud)
我绝对没有什么想法(*C::*),(**C::*),(C::**)和(C::***)的意思.我想的一个例子test<decltype(f)>,其value将等于2,3, …
问题如下C++14:
FV&& valid_f,FI&& invalid_f和参数Args&&... argsapply_on_validity应该应用该函数valid_fargsstd::forward<FV>(valid_f)(std::forward<Args>(args)...)std::forward<FV>(invalid_f)(std::forward<Args>(args)...)是有效的表达式,apply_on_validity应适用invalid_f于argsapply_on_validity应该什么都不做我猜代码看起来像这样:
template <class FV, class FI, class... Args, /* Some template metaprog here */>
void apply_on_validity(FV&& valid_f, FI&& invalid_f, Args&&... args)
{
// Apply valid_f by default
std::forward<FV>(valid_f)(std::forward<Args>(args)...);
}
template <class FV, class FI, class... Args, /* Some template metaprog here */>
void apply_on_validity(FV&& valid_f, FI&& invalid_f, Args&&... args)
{
// …Run Code Online (Sandbox Code Playgroud) 考虑以下功能
template <class... T, class... U>
void f(std::tuple<T...> t, std::tuple<U...> u)
{
std::cout << sizeof...(T) << " " << sizeof...(U) << std::endl;
}
int main(int argc, char* argv[])
{
f({3, 3.5, "Hello World!"}, {'a', std::string("b")}); // Fails
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在C++ 17中是否有任何方法可以修改函数签名,以便标记为"Fails"的行可以工作?(保持该线相同).
我想知道在编译时获取可变参数模板类的第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)
非常感谢你.