如果我们去维基百科关于C++运算符的文章,我们举个例子:
Addition : a + b -> T T::operator +(const T& b) const;
Run Code Online (Sandbox Code Playgroud)
所以运算符返回类型为T 的非const.如果我们看一下这个指南,作者说返回类型应该是一个const,以避免以下语法:
(a+b) = c
Run Code Online (Sandbox Code Playgroud)
现在假设这个语法不会打扰我,并认为a和b是大数组.从"纯"性能的角度来看,返回类型中缺少const关键字是否会阻止编译器的优化(g ++和intel icpc with -O3)?如果aswer是"是",为什么?
请考虑以下代码:
template<unsigned int... TSIZE>
struct Base {};
template<unsigned int TORDER, unsigned int TDIM>
struct Derived : public Base</* TDIM, TDIM, ... TDIM (TORDER times) */> {};
Run Code Online (Sandbox Code Playgroud)
您是否认为在此示例的第二行上正确生成Base的模板参数存在技巧?例如,我想Derived<3, 5>继承Base<5, 5, 5>.怎么做 ?
我从来没有实现类似STL的迭代器,我试图理解如何基于指针实现一个非常基本的东西.一旦我有了这门课程,我就可以修改它来做更复杂的事情.因此,这是第一步,我需要它坚如磐石才能理解如何编写自己的迭代器(没有boost).
我写了下面的代码,我知道它有错误.你能帮助我正确设计一个受其启发的Random Access Iterator类:
template<Type> class Container<Type>::Iterator : public std::iterator<random_access_iterator_tag, Type>
{
// Lifecycle:
public:
Iterator() : _ptr(nullptr) {;}
Iterator(Type* rhs) : _ptr(rhs) {;}
Iterator(const Iterator &rhs) : _ptr(rhs._ptr) {;}
// Operators : misc
public:
inline Iterator& operator=(Type* rhs) {_ptr = rhs; return *this;}
inline Iterator& operator=(const Iterator &rhs) {_ptr = rhs._ptr; return *this;}
inline Iterator& operator+=(const int& rhs) {_ptr += rhs; return *this;}
inline Iterator& operator-=(const int& rhs) {_ptr -= rhs; return *this;}
inline Type& operator*() {return *_ptr;}
inline …Run Code Online (Sandbox Code Playgroud) 以下教学示例说明了我的问题:
#include <iostream>
#include <cmath>
template<class Function, class... Args>
double apply(Function f, Args... args)
{
return f(args...);
}
template<class Function, class... Args>
double applybis(Function f, Args... args)
{
return f(std::sin(args...));// <- How to apply a function to
// each variadic parameter and
// return a modified variadic list ?
}
int main(int argc, char* argv[])
{
std::cout<<apply(static_cast<double(*)(double)>(std::sin), 3.)<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何通过将函数应用于每个组件并返回修改后的可变参数列表来"转换"可变参数列表?(有没有办法编写applybis函数而不修改其当前签名?)
检测架构是否在C++中使用一个或两个补码表示的最可靠方法是什么?
在一个补码架构上考虑以下代码:
int zero = 0;
int negzero = -0;
std::cout<<(negzero < zero)<<std::endl;
std::cout<<(negzero <= zero)<<std::endl;
std::cout<<(negzero == zero)<<std::endl;
std::cout<<(~negzero)<<(~zero)<<std::endl;
std::cout<<(1 << negzero)<<std::endl;
std::cout<<(1 >> negzero)<<std::endl;
Run Code Online (Sandbox Code Playgroud)
什么是最快的方法(在常见的现代架构上的cpu周期方面),len从位置开始生成位设置为1 的掩码pos:
template <class UIntType>
constexpr T make_mask(std::size_t pos, std::size_t len)
{
// Body of the function
}
// Call of the function
auto mask = make_mask<uint32_t>(4, 10);
// mask = 00000000 00000000 00111111 11110000
// (in binary with MSB on the left and LSB on the right)
Run Code Online (Sandbox Code Playgroud)
另外,是否有任何编译器内在函数或BMI函数可以帮助?
这是我的示例类:
template<typename T> class MyClassVector
{
public:
inline const std::vector<T>& data() const
{
return _data;
}
protected:
std::vector<T> _data;
};
template<typename T, unsigned int SIZE> class MyClassArray
{
public:
inline const /* SOMETHING */ data() const
{
return _data; // OR SOMETHING ELSE
}
protected:
T _data[SIZE];
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:MyClassArray类的MyClassVector data()函数的等价物是什么,它返回对底层_data容器的常量引用?
非常感谢你 !
我想知道如何编写一个type_traits类来检测两个类型是否是同一个模板类的特化.最大的问题是它应该适用于混合类型/非类型模板类,如:
template <typename T, std::size_t N>
class MyClass {};
Run Code Online (Sandbox Code Playgroud)
有可能设计出这样的东西吗?
type-traits template-specialization template-meta-programming c++11
在typeid允许分配一个唯一的std::type_index在运行时每个类型.我想做同样的事情,静态地使用两个元函数:
// Get a unique integral number associated with the provided type
template <class T>
struct encode_type
{
using type = T;
static constexpr std::size_t value = /* Metaprogramming magic */;
};
// Get the type uniquely associated with the provided value
template <std::size_t V>
struct decode_type
{
static constexpr std::size_t value = V;
using type = /* Metaprogramming magic */;
};
Run Code Online (Sandbox Code Playgroud)
有没有办法在C++ 11中做到这一点?
c++ ×9
c++11 ×6
templates ×3
optimization ×2
architecture ×1
arrays ×1
bitmask ×1
integer ×1
iterator ×1
performance ×1
reference ×1
standards ×1
stl ×1
type-traits ×1
types ×1