假设我有以下元函数:
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)
我看到两种可能性:
编译器每次看到它都必须做一些工作typename std::remove_reference<T>::type.使用中间别名具有某种"缓存"行为,这允许编译器只执行一次工作.
编译时性能是根据编译器必须执行的模板实例化的数量来衡量的.因为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
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++(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)
我知道的
上面的代码无法编译:
但它成功编译:
此外,如果我将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) #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::tuple和hana::range_c,但hana::range_c不被认为是一个序列,这是一个要求hana::zip.这个决定背后的原因是什么?我如何(惯用)在尊重这个决定的同时实现我的目标?
c++ ×4
boost-hana ×2
boost-mpl ×1
c++14 ×1
clang ×1
gcc ×1
nested-class ×1
reflection ×1
templates ×1