无法将 std::time_get.get_date 函数作为参数传递给另一个函数
Inability to pass an std::time_get.get_date function as argument to another function
我正在尝试将一个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;
}
相关文章:
- C++中是否有任何函数等同于python中字典的get函数?
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 使用"push_back"作为带有"get"函数C++的向量
- 从 unique_ptr::get 发送指针作为指针引用进入函数
- c++ 在类中声明私有的向量,但有一个 get 函数
- c++为什么ifstream get()函数返回无法识别的特殊字符
- 使用Set/Get-like方法或GetSet组合函数
- 为什么 std::get<T> 其中 T 是调用 constexpr 函数失败的结果?
- 如何在输入成员函数中使用 get()
- C++ :: 设置值函数在函数内设置,但后跟 Get 函数,设置值消失了
- 为什么 ADL 不能使用 std::get 解析为正确的函数
- 在类中使用映射并通过其他类的 get() 和 set() 函数访问值
- C++11: 如何编写一个像 Get 一样工作的模板函数<tuple>...但是收到参数包?
- get 函数返回无效值
- cin.get()函数在C 中
- 通过引用将私有类实例传递到另一个类 C++ 的"get"函数中
- 类成员函数 get 不会返回值
- ifstream 函数"get"如何改变其字符参数?
- 重载运算符()与成员函数get()
- 输入流函数get在c++中