小编Cor*_*rey的帖子

表达式模板没有完全内联

我已经完成了第一个数学库版本,下一步我想转向表达式模板来提高代码的性能.但是,我的初步结果与我的预期不同.我正在MSVC 2010中编译,在vanilla Release模式下(并且可以使用C++ 0x).

对于我将向您展示的大量代码提前道歉,在让人们看到我正在做的事情的同时,我尽可能地做到这一点.分析框架:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <limits>
#include <type_traits>
#include <vector>

namespace math
{
    class vector; // to be determined

    std::ostream& operator<<(std::ostream& stream, const vector& vec)
    {
        for (std::size_t i = 0; i < 4; ++i)
            stream << vec[i] << " ";

        return stream;
    }
}

// test framework
typedef std::vector<math::vector> array_type[3];
typedef std::vector<math::vector> vector_type;

float generate_float()
{
    return static_cast<float>(rand());
}

math::vector generate_vector()
{
    return math::vector(generate_float(), generate_float(),
                        generate_float(), generate_float());
} …
Run Code Online (Sandbox Code Playgroud)

c++ optimization inlining expression-templates

14
推荐指数
1
解决办法
544
查看次数

C++模板宏快捷方式

通常在使用模板时,最终会得到以下内容:

template <T>
class the_class
{
public:
   // types
   typedef T value_type;
   typedef const value_type const_value_type;

   typedef value_type& reference;
   typedef const_value_type& const_reference;

   typedef value_type* pointer;
   typedef const_value_type* const_pointer;

   ...
};
Run Code Online (Sandbox Code Playgroud)

不过,这是很多相同的东西,复制到许多不同的模板类.是否值得创建类似的东西:

// template_types.h

#define TEMPLATE_TYPES(T) \
       typedef T value_type; \
       typedef const value_type const_value_type; \
       typedef value_type& reference; \
       typedef const_value_type& const_reference; \
       typedef value_type* pointer; \
       typedef const_value_type* const_pointer;
Run Code Online (Sandbox Code Playgroud)

所以我的班级变成了:

#include "template_types.h"

template <typename T>
class the_class
{
public:
   TEMPLATE_TYPES(T)
   ...
};
Run Code Online (Sandbox Code Playgroud)

这看起来更干净,并且在我制作其他模板类时避免重复.这是一件好事吗?或者我应该避免这种情况,只是复制粘贴typedef?

c++ macros templates

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

关于继承/方法重写C++的问题

class Class1
{
public:
    void print()
    {
        cout << "test" << endl;
    }

    void printl()
    {
        print();
    }
};

class Class2 : public Class1
{
public:
    void print()
    {
        cout << "test2" << endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

为什么print()不会在Class2中被覆盖,是否有任何方法可以像这样覆盖一个函数?(没有虚函数).谢谢

    Class2 t;
    t.printl();
Run Code Online (Sandbox Code Playgroud)

c++

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