无法将 std::time_get.get_date 函数作为参数传递给另一个函数

Inability to pass an std::time_get.get_date function as argument to another function

本文关键字:函数 get 参数传递 另一个 time date std      更新时间:2023-10-16

我正在尝试将一个std::time_get.get_date函数作为参数传递给另一个函数。

我想这样做的原因是将其他 std::time_get 函数(具有相同的签名(传递给另一个函数。

但是,我收到编译错误。 --编辑 -- 在线版本如下:
http://coliru.stacked-crooked.com/a/8f8df97ff106e9be

/** Passing the time_get<char>.get_date function member to a function
*/
#include <locale>           /// time_get<char>
#include <iterator>         /// istreambuf_iterator<char>
#include <ctime>            /// tm
#include <ios>              /// ios_base
#include <iostream>
using namespace std;
using InIt = istreambuf_iterator<char>;
using IB = ios_base;
using ST = IB::iostate;
using TG = const time_get<char>;
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*);
/// Addresses of functions in time_get<char>
auto gd = &TG::get_date;
/// pGet function arguments
InIt frm (cin), til;
ST st;
tm t;
locale loc;                     /// classic "C" locale
TG& tg =                        /// time_get<char> facet
    use_facet<TG> (loc);

void getdtpart(TG& tg, pGet fget)
{
    (tg.*(fget)) (frm, til, cin, st, &t);
}

int main()
{
    getdtpart(tg, gd);
    cout << "Completed" << endl;
}

编译错误为:

main.cpp: In function 'void getdtpart(TG&, pGet)':
main.cpp:34:40: error: invalid conversion from 'TG* {aka const std::__cxx11::time_get<char>*}' to 'TG* {aka std::__cxx11::time_get<char>*}' [-fpermissive]
     (tg.*(fget)) (frm, til, cin, st, &t);
                                        ^
main.cpp: In function 'int main()':
main.cpp:40:21: error: cannot convert 'std::istreambuf_iterator<char, std::char_traits<char> > (std::__cxx11::time_get<char>::*)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, std::_Ios_Iostate&, tm*) const' to 'pGet {aka std::istreambuf_iterator<char, std::char_traits<char> > (std::__cxx11::time_get<char>::*)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, std::_Ios_Iostate&, tm*)}' for argument '2' to 'void getdtpart(TG&, pGet)'
     getdtpart(tg, gd);
                     ^

很明显,这是因为"const"的存在与否而发生的,但我不确定如何解决这个问题。我该怎么做?

似乎问题是在两个地方使用/不使用const

第一部分是pGet它应该捕获get_date的签名,一个const成员函数。因此,请在末尾添加const

using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;

另一部分是TG类型中const的us,这似乎与定义pGet时使用TG冲突。

分解掉这一点,并将一些变量移动到 main 以避免名称阴影,我得到这个版本来编译:

/** Passing the time_get<char>.get_date function member to a function
*/
#include <locale>           /// time_get<char>
#include <iterator>         /// istreambuf_iterator<char>
#include <ctime>            /// tm
#include <ios>              /// ios_base
#include <iostream>
using namespace std;
using InIt = istreambuf_iterator<char>;
using IB = ios_base;
using ST = IB::iostate;
using TG = time_get<char>;
using pGet = InIt (TG::*) (InIt, InIt, IB&, ST&, tm*) const;
/// Addresses of functions in time_get<char>
auto gd = &TG::get_date;
/// pGet function arguments
InIt frm (cin), til;
ST st;
tm t;

void getdtpart(const TG& tg, pGet fget)
{
    (tg.*(fget)) (frm, til, cin, st, &t);
}

int main()
{
   locale loc;                     /// classic "C" locale
   const TG& tg =                        /// time_get<char> facet
       use_facet<TG> (loc);
    getdtpart(tg, gd);
    cout << "Completed" << endl;
}