gpt4 book ai didi

c++ - 制作一个优化未使用部分的元组样式类

转载 作者:行者123 更新时间:2023-11-28 07:42:33 25 4
gpt4 key购买 nike

这更多是关于 C++ 编译器如何处理 const typeid 调用的问题。

您好!我正在尝试制作一个元组样式的类,其配置方式使我不必重写一堆具有特化的代码。

所以这是一般的想法:

struct null_type{};

template <typename T1,typename T2=null_type,typename T3=null_type>
class ptestclass
{
private:
template<typename K1,typename K2,typename K3>
class barclass
{
public:
static inline void bar(std::tuple<K1,K2,K3>& vals,K1* otherval1,K2* otherval2,K3* otherval3)
{
Foo(tr1::get<0>(vals),*otherval1);
Foo(tr1::get<1>(vals),*otherval2);
Foo(tr1::get<2>(vals),*otherval3);
}
};
template<typename K1,typename K2>
class barclass<K1,K2,null_type>
{
public:
static inline void bar(std::tuple<K1,K2,null_type>& vals,K1* otherval1,K2* otherval2,null_type* otherval3)
{
Foo(tr1::get<0>(vals),*otherval1);
Foo(tr1::get<1>(vals),*otherval2);
}
};
template<typename K1>
class barclass<K1,null_type,null_type>
{
public:
static inline void bar(std::tuple<K1,null_type,null_type>& vals,K1* otherval1,null_type* otherval2,null_type* otherval3)
{
Foo(tr1::get<0>(vals),*otherval1);
}
};

/*
*Old Bar function...much more readable than bar class, but you cannot partially specialize
*member functions of a class
*
void inline bar(std::tuple<T1,T2,T3> otherval)
{
if (typeid(T1) != typeid(null_type))//constant check hopfully optomized out
{
Foo(vals.get(1),otherval.get(1));
}
if (typeid(T2) != typeid(null_type))//constant check hopfully optomized out
{
Foo(vals.get(2),otherval.get(2));
}
if(typeid(T3) != typeid(null_type))//constant check hopfully optomized out
{
Foo(vals.get(3),otherval.get(3));
}

}
*/
std::tuple<T1,T2,T3> vals;



template<typename K>
void static inline Foo(K& val,K& otherval)
{
//inlineable, short function that is called many (millions) of times per iteration
val += otherval;
}

template<>
void inline Foo<null_type>(null_type& val,null_type& otherval)
{
//inlineable, short function that is called many (millions) of times per iteration
throw "Foo called on null type";
}

public:
ptestclass()
{
printf("made object");
}
void one_iteration(T1* otherval1,T2* otherval2,T3* otherval3,size_t count)
{
for (int i = 0; i < count; ++i)
{
barclass<T1,T2,T3>::bar(vals,otherval1+i,otherval2+i,otherval3+i);
}
}
};

//exposed public class with specialized one_iteration interfaces
template <typename T1,typename T2=null_type,typename T3=null_type>
class testclass : public ptestclass<T1,T2,T3>
{
public:
void one_iteration(T1* otherval1,T1* otherval2,T1* otherval3,size_t count)
{
ptestclass::one_iteration(otherval1,otherval2,otherval3,count);
}
};

template <typename T1>
class testclass<T1,null_type,null_type> : public ptestclass<T1,null_type,null_type>
{
public:
void one_iteration(T1* otherval1,size_t count)
{
ptestclass::one_iteration(otherval1,NULL,NULL,count);
}
};

所以我的问题是这种优化甚至可以在 C++ 中实现吗?如果不是,对我来说,在子节点上使用继承模型而不是在这个级别使用模板可能更有意义。但是,我试图避免持续检查指定类型的数量和间接成本。

我将开始深入研究程序集,看看编译器是否就是这样做的...以防万一这不是标准化行为,我正在使用 Microsoft Visual C++ 编译器 10.0。

最佳答案

我想我在发表之前的评论时误解了你的问题。

假设你可以使用 c++11,或者你可以使用 boost,你可以使用类似 !std::is_same< T1, null_type >::value /*or boost::is_same...*/ 的东西而不是 typeid(T1) != typeid(null_type)。这使用 TMP 解析为编译时常量,大多数编译器可以毫不费力地对其进行优化。

This is more of a question of how the C++ compiler handles const typeid calls.

我没有回答这个具体问题,但如果我了解您实际要查找的内容,那么以上内容就足够了。

关于c++ - 制作一个优化未使用部分的元组样式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15503459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com