【发布时间】:2021-11-11 21:15:04
【问题描述】:
我在Understanding how the function traits template works. In particular, what is the deal with the pointer to member function 上找到了一个解决方案,可以使用模板获取函数参数类型的类型。但以下代码在 VS2019 上得到“错误 C2760:语法错误:意外令牌'
有什么建议吗?
#include <functional>
#include <tuple>
#include <type_traits>
class CChTest
{
public:
CChTest()
{
}
bool ChDeneme(int a, int b)
{
return false;
}
};
template<typename> struct function_traits;
template <typename Function>
struct function_traits : public function_traits<
decltype(&std::remove_reference<Function>::type::operator())>
{
};
template <
typename ClassType,
typename ReturnType,
typename... Arguments>
struct function_traits<
ReturnType(ClassType::*)(Arguments...) const>
: function_traits<ReturnType(*)(Arguments...)>
{
};
template <
typename ClassType,
typename ReturnType,
typename... Arguments>
struct function_traits<
ReturnType(ClassType::*)(Arguments...)>
: function_traits<ReturnType(*)(Arguments...)>
{
};
template <
typename ReturnType,
typename... Arguments>
struct function_traits<
ReturnType(*)(Arguments...)>
{
typedef ReturnType result_type;
template <std::size_t Index>
using argument = typename std::tuple_element<
Index,
std::tuple<Arguments...>>::type;
static const std::size_t arity = sizeof...(Arguments);
};
template <
typename _ChClass,
typename _ChFunction>
void ChTemplate(
_ChClass cl,
_ChFunction fn)
{
typename function_traits<_ChFunction>::argument<0> a;
}
int main()
{
CChTest ChTest;
ChTemplate(&ChTest, &CChTest::ChDeneme);
return 0;
}
【问题讨论】:
-
Paraphrasing errors 有缺失信息的问题,比如出错的行号。请阅读正确的minimal reproducible example 的重要性。
-
顺便说一句,像
_ChClass(前导下划线后跟大写字母)这样的标识符是reserved to the implementation for any use。使用一个就是自找麻烦。所以我建议修复它以提高便携性。谁知道呢……你的问题可能会神奇地得到解决。 -
还是同样的错误。
-
@TedLyngmo,我将代码上的大写字母改为小写字母。所以,我得到了同样的错误。