小编Lou*_*nne的帖子

通过缓存元函数优化编译时性能

假设我有以下元函数:

template <typename T>
struct make_pair {
    using type = std::pair<
        typename std::remove_reference<T>::type,
        typename std::remove_reference<T>::type
    >;
};
Run Code Online (Sandbox Code Playgroud)

相反,它会提高编译速度(或其他)吗?

template <typename T>
struct make_pair {
    using without_reference = typename std::remove_reference<T>::type;
    using type = std::pair<without_reference, without_reference>;
};
Run Code Online (Sandbox Code Playgroud)

我看到两种可能性:

  1. 编译器每次看到它都必须做一些工作typename std::remove_reference<T>::type.使用中间别名具有某种"缓存"行为,这允许编译器只执行一次工作.

  2. 编译时性能是根据编译器必须执行的模板实例化的数量来衡量的.因为std::remove_reference<T>::type引用的类型相同std::remove_reference<T>::type,所以在这两种情况下只需要一个模板实例化,因此两个实现都是等效的WRT编译时性能.

我认为B是对的,但我想确定一下.如果答案结果是编译器特定的,我最感兴趣的是知道Clang和GCC的答案.

编辑:

我对测试程序的编译进行了基准测试,以便使用一些数据.测试程序做了类似的事情:

template <typename ...> struct result;    

template <typename T>
struct with_cache {
    using without_reference = typename std::remove_reference<T>::type;
    using type = result<without_reference, ..., without_reference>;
};

template <typename T>
struct without_cache {
    using type …
Run Code Online (Sandbox Code Playgroud)

c++ templates instantiation boost-mpl template-meta-programming

13
推荐指数
1
解决办法
685
查看次数

是否可以使用Boost Hana反省方法?

Boost Hana提供了以简单而美观的方式对类成员字段进行内省的能力:

// define:

struct Person {
  std::string name;
  int age;
};

// below could be done inline, but I prefer not polluting the 
// declaration of the struct
BOOST_HANA_ADAPT_STRUCT(not_my_namespace::Person, name, age);

// then:

Person john{"John", 30};
hana::for_each(john, [](auto pair) {
  std::cout << hana::to<char const*>(hana::first(pair)) << ": "
            << hana::second(pair) << std::endl;
});
Run Code Online (Sandbox Code Playgroud)

但是,文档仅提及成员字段.我也想对方法进行反思.我试图用一种方法天真地扩展示例:

struct Foo {
    std::string get_name() const { return "louis"; }
};

BOOST_HANA_ADAPT_STRUCT(::Foo, get_name);
Run Code Online (Sandbox Code Playgroud)

这编译.但是,只要我尝试使用它,使用类似于上面的代码(for_each...),我就会遇到很多编译错误.由于没有显示方法内省的例子,我想知道它是否得到支持.

c++ reflection generic-programming c++14 boost-hana

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

多重继承隐藏的嵌套类

这段代码是否有效C++(11)?

struct Base {
    template <typename>
    struct nested;
};
struct Derived1 : Base { };
struct Derived2 : Base { };
struct Derived3 : Derived1, Derived2 { };

typedef Derived3::nested<int> xxx;
Run Code Online (Sandbox Code Playgroud)

我知道的

上面的代码无法编译:

  • Apple LLVM 5.0(clang-500.2.75)
  • Clang 3.4

但它成功编译:

  • gcc 4.9.0 20131110(实验性)
  • gcc 4.8

此外,如果我将nested类型更改为非模板类​​型,即

struct Base {
    struct nested;
};
...
typedef Derived3::nested xxx;
Run Code Online (Sandbox Code Playgroud)

然后它适用于上述编译器.

[edit]nested模板结构更改为模板别名也不会改变任何内容;

template <typename> struct dependent { struct type; };
struct Base {
    template <typename T>
    using nested = typename …
Run Code Online (Sandbox Code Playgroud)

c++ gcc multiple-inheritance clang nested-class

7
推荐指数
1
解决办法
322
查看次数

为什么`boost :: hana :: range_c`不是序列?

#include <string>
#include <utility>
#include <vector>
#include <boost/hana.hpp>
namespace hana = boost::hana;

template <typename ...T>
void indexed_T_work(T&& ...args)
{
    auto indices = hana::range_c<std::size_t, 0, sizeof...(T)>;
    auto types = hana::make_tuple(std::forward<T>(args)...);
    hana::for_each(
        hana::zip(indices, types)
      , [](auto&& pair_) { /* Do index-dependent work with each `T` */ }
        );
}

int main()
{
    indexed_T_work(5, 13, std::vector<std::string>{}, 32.f, 42, "foo");
}
Run Code Online (Sandbox Code Playgroud)

我想用hana::zipa hana::tuplehana::range_c,但hana::range_c不被认为是一个序列,这是一个要求hana::zip.这个决定背后的原因是什么?我如何(惯用)在尊重这个决定的同时实现我的目标?

c++ boost-hana

5
推荐指数
1
解决办法
241
查看次数