小编Vin*_*ent的帖子

重载运算符:const vs非const返回类型:性能的任何差异?

如果我们去维基百科关于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是"是",为什么?

c++ optimization performance operator-overloading

9
推荐指数
1
解决办法
2109
查看次数

您可以从大小及其内容生成可变参数模板包吗?

请考虑以下代码:

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>.怎么做 ?

c++ templates variadic-templates c++11

9
推荐指数
1
解决办法
231
查看次数

基于指针的基本随机访问迭代器的代码?

我从来没有实现类似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)

c++ iterator stl

9
推荐指数
2
解决办法
1万
查看次数

如何将函数应用于可变参数列表的每个组件并返回可变参数列表?

以下教学示例说明了我的问题:

#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++ templates metaprogramming variadic-templates c++11

9
推荐指数
1
解决办法
272
查看次数

在C++中检测一个或两个补码架构?

检测架构是否在C++中使用一个或两个补码表示的最可靠方法是什么?

c++ architecture twos-complement ones-complement c++11

9
推荐指数
1
解决办法
1525
查看次数

一个补码架构上的负零行为?

在一个补码架构上考虑以下代码:

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)
  • 代码会产生什么输出?
  • 标准定义了哪些行,哪些行依赖于实现,哪些行是未定义的行为?

c++ standards integer ones-complement c++11

9
推荐指数
1
解决办法
389
查看次数

从位置i开始生产具有n个掩模的最快方法

什么是最快的方法(在常见的现代架构上的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函数可以帮助?

c++ optimization bit-manipulation bitmask

9
推荐指数
2
解决办法
852
查看次数

将const引用返回给C数组?

这是我的示例类:

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容器的常量引用?

非常感谢你 !

c++ arrays reference

8
推荐指数
1
解决办法
3258
查看次数

检测两种类型是否是同一类模板的特化?

我想知道如何编写一个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

8
推荐指数
1
解决办法
212
查看次数

元函数将类型转换为整数,反之亦然

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++ templates types template-meta-programming c++11

8
推荐指数
1
解决办法
1559
查看次数