1

I found a solution on Understanding how the function traits template works. In particular, what is the deal with the pointer to member function to get type of function parameters types by using templates. But following code gets "error C2760: syntax error: unexpected token '<', expected 'declaration'" on VS2019. It works on GCC ? When I use c++17 or c++20 gets same error. This seems vs2019 bug ?

Any suggestions ?

#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;
}
VeNToR
  • 33
  • 6
  • 1
    Paraphrasing errors has the problem of missing information, such as the line number where the error occurs. Please read about the importance of a proper [mre]. – StoryTeller - Unslander Monica Sep 16 '21 at 22:33
  • 1
    As somewhat of an aside, identifiers like `_ChClass` (leading underscore followed by a capital letter) are [reserved to the implementation for any use](https://stackoverflow.com/q/228783/817643). To use one is asking for trouble. So I recommend fixing that for portability. And who knows... your problem may then get magically solved. – StoryTeller - Unslander Monica Sep 16 '21 at 22:35
  • still same error. – VeNToR Sep 16 '21 at 22:40
  • 1
    @TedLyngmo, I changed capital letters to lower letters on my codes. So, I get same error. – VeNToR Sep 16 '21 at 22:46

1 Answers1

2

You need to add the template keyword to make argument a dependent template name:

template <
    typename    _ChClass,
    typename    _ChFunction>
void ChTemplate(
    _ChClass    cl,
    _ChFunction fn)
{
    typename function_traits<_ChFunction>::template argument<0>  a;
    //                                     ^^^^^^^^
}

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108