C++ 如何删除typename中每个元素的常量引用修饰符。。。T
我刚开始用可变版本替换一些旧模板,以避免代码重复(或难看的宏),从而避免参数数量可变的重载 我遇到的一个问题还没有解决:如何从属于“混合类型名…”的任何类型中删除const&呢 以下是我的例子:C++ 如何删除typename中每个元素的常量引用修饰符。。。T,c++,variadic-templates,stdtuple,C++,Variadic Templates,Stdtuple,我刚开始用可变版本替换一些旧模板,以避免代码重复(或难看的宏),从而避免参数数量可变的重载 我遇到的一个问题还没有解决:如何从属于“混合类型名…”的任何类型中删除const&呢 以下是我的例子: #include <tuple> // old version to be replaced namespace old_version { template<typename T> struct struct_const_der
#include <tuple>
// old version to be replaced
namespace old_version
{
template<typename T> struct struct_const_deref { typedef T Type; };
template<typename T> struct struct_const_deref < const T& > { typedef T Type; };
template < typename F, typename T1 >
void exec(F* pObj, void(F::*pFct)(T1))
{
typename struct_const_deref<T1>::Type t1;
// some code that fills t1
(pObj->*pFct)(t1);
}
template < typename F, typename T1 , typename T2 >
void exec(F* pObj, void(F::*pFct)(T1, T2))
{
typename struct_const_deref<T1>::Type t1;
typename struct_const_deref<T2>::Type t2;
// some code that fills t1, t2
(pObj->*pFct)(t1, t2);
}
}
// new variadic version
namespace variadic
{
template< typename... Args > struct struct_const_deref_tuple { typedef std::tuple< Args... > Tuple; };
template< typename... Args > struct struct_const_deref_tuple < const Args&... > { typedef std::tuple< Args... > Tuple; };
template < typename F, typename... Args >
void exec(F* pObj, void(F::*pFct)(Args... args))
{
typename struct_const_deref_tuple< Args... >::Tuple tN;
// some code that fills tN
// some helper template that 'extracts' the tuple and calls (pObj->*pFct)(ExtractedArgs...)
}
}
struct Test
{
void foo(int i) {}
void bar(const float& f, const int& i) {}
void buu(const float& f, int i) {}
};
int main(int argc, char* argv[])
{
Test t;
old_version::exec(&t, &Test::foo); // ok
old_version::exec(&t, &Test::bar); // ok
old_version::exec(&t, &Test::buu); // ok
variadic::exec(&t, &Test::foo); // ok
variadic::exec(&t, &Test::bar); // ok
variadic::exec(&t, &Test::buu); // fails (the struct_const_deref_tuple does not 'catch' my case; 'tN' will be std::tuple<const float&, int>
return 0;
}
因为tN变成了
std::tuple<const float&, int>
std::tuple
但这显然是必须的
std::tuple<float, int>
std::tuple
让我工作
欢迎任何想法:)根本问题是。此外,为了简单起见,您可以使用type_traits中的std::decay(c++11)或std::decay_t(c++14)。下面的代码应该用c++14编译
#include <tuple>
#include <type_traits>
// new variadic version
namespace variadic
{
template < typename F, typename... Args >
void exec(F* pObj, void(F::*pFct)(Args... args))
{
std::tuple<std::decay_t<Args>...> tN;
// some code that fills tN
// some helper template that 'extracts' the tuple and calls (pObj->*pFct)(ExtractedArgs...)
}
}
struct Test
{
void foo(int i) {}
void bar(const float& f, const int& i) {}
void buu(const float& f, int i) {}
};
int main(int argc, char* argv[])
{
Test t;
variadic::exec(&t, &Test::foo); // ok
variadic::exec(&t, &Test::bar); // ok
variadic::exec(&t, &Test::buu); // ok
return 0;
}
#包括
#包括
//新的可变版本
名称空间变量
{
模板
void exec(F*pObj,void(F::*pFct)(Args…Args))
{
std::元组tN;
//一些填充tN的代码
//“提取”元组并调用(pObj->*pFct)(ExtractedArgs…)的一些辅助模板
}
}
结构测试
{
void foo(int i){}
空条(常数浮点和f,常数整数和i){}
void buu(常量浮点&f,int i){}
};
int main(int argc,char*argv[])
{
试验t;
变量::exec(&t,&Test::foo);//确定
变量::exec(&t,&Test::bar);//确定
变量::exec(&t,&Test::buu);//确定
返回0;
}
请看一下这个
#include <type_traits>
template<typename T>
struct RemoveConst;
template<typename T>
struct RemoveConst{
using type = typename std::remove_const<T>::type;
};
template<typename T>
struct RemoveConst<const T>{
using type = typename RemoveConst<T>::type;
};
template<typename T>
struct RemoveConst<const T*>{
using type = typename RemoveConst<T>::type*;
};
template<typename T>
struct RemoveConst<T* const>{
using type = typename RemoveConst<T>::type*;
};
template<template <typename...> class T, typename... Args>
struct RemoveConst<T<Args...>>{
using type = T<typename RemoveConst<Args>::type...>;
};
#包括
模板
结构RemoveConst;
模板
结构RemoveConst{
使用type=typename std::remove_const::type;
};
模板
结构RemoveConst{
使用type=typename RemoveConst::type;
};
模板
结构RemoveConst{
使用type=typename RemoveConst::type*;
};
模板
结构RemoveConst{
使用type=typename RemoveConst::type*;
};
模板
struct RemoveCons他们的速度很快,正是我想要的,非常感谢!对您的答案有一个建议:您可以将注释//fails(…)更改为类似于//now ok too或类似的答案代码:)@Xor我在一些评论之后更改了它。非常感谢。退房并离开。甚至是C++20。
#include <type_traits>
template<typename T>
struct RemoveConst;
template<typename T>
struct RemoveConst{
using type = typename std::remove_const<T>::type;
};
template<typename T>
struct RemoveConst<const T>{
using type = typename RemoveConst<T>::type;
};
template<typename T>
struct RemoveConst<const T*>{
using type = typename RemoveConst<T>::type*;
};
template<typename T>
struct RemoveConst<T* const>{
using type = typename RemoveConst<T>::type*;
};
template<template <typename...> class T, typename... Args>
struct RemoveConst<T<Args...>>{
using type = T<typename RemoveConst<Args>::type...>;
};