- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个“功能迭代器”类型的类——也就是说,迭代器返回的值首先由一个函数对象处理,它是模板参数。此模板参数的默认值为“平凡仿函数”,它只返回它收到的参数。
当我添加 const
成员函数时出现问题,该成员函数应返回对元素的 const
引用。为简单起见,假设我们有一个“前向迭代器”并希望有一个函数“peek_next”,它允许查看下一个元素,但不能更改它。这是我的看法(这是最小的可编译示例):
#include <functional>
#include <iostream>
#include <vector>
/**
* This is an empty functor which just returns back its argument.
*/
template <class T>
class basic_functor: public std::unary_function<T, T>
{
public:
typedef basic_functor self;
basic_functor()
{}
/**
* Returns back the argument passed to it. const version
*/
inline const T& operator()(const T& arg) const
{ return arg; }
/**
* Returns back the argument passed to it.
*/
inline T& operator()(T& arg)
{ return arg; }
};// class basic_functor
/**
* @param S type of the elements.
* @param BaseIterator type of the iterator it builds upon.
* @param functor which is applied to the element to which iterator points.
* by default it is empty functor -- the element itself is returned.
*/
template <typename S,
typename BaseIterator,
typename Functor=basic_functor<S> >
class iterator_functional:
public std::iterator<std::forward_iterator_tag,
typename Functor::result_type >
{
public:
///type defining itself
typedef iterator_functional self;
///type of the functor
typedef Functor functor_type;
///type of linear iterator
typedef BaseIterator base_iterator_type;
///type for rebinding the iterator with another functor
template <typename F>
struct rebind
{
typedef iterator_functional<S, base_iterator_type, F> other;
};
/**
* Constructor.
*/
iterator_functional(base_iterator_type it,
functor_type funct = functor_type()):
m_it(it),
m_functor(funct)
{
}
/**
* Copying constructor.
* @param other source of the data.
*/
iterator_functional(const self&other):
m_it(other.m_it),
m_functor(other.m_functor)
{ }
/**
* Provides access to element to which iterator points.
* @return element to which iterator points to.
*/
inline typename self::reference operator*()
{ return m_functor(*m_it); }
/**
* Provides access to element to which iterator points.
* @return element to which iterator points to.
*/
inline const typename self::reference operator*() const
{ return m_functor(*m_it); }
// Will work only when the code below is 'unlocked'
#if 0
/**
* Returns reference to the next element.
*/
inline typename self::reference peek_next()
{
auto temp_it = m_it;
++temp_it;
return
m_functor(*temp_it);
}//iterator_type_const
#endif
/**
* Returns reference to the next element.
*/
inline const typename self::reference peek_next() const
{
auto temp_it = m_it;
++temp_it;
return
m_functor(*temp_it);
}//iterator_type_const
private:
base_iterator_type m_it;
functor_type m_functor;
};//iterator_functional;
typedef std::vector<int>::iterator v_int_iterator;
typedef std::vector<int>::const_iterator v_int_const_iterator;
typedef iterator_functional<int, v_int_iterator> iterator_type;
typedef iterator_functional<const int, v_int_const_iterator> iterator_type_const;
int main()
{
std::vector<int> vec_1({1, 2, 3, 4});
const std::vector<int> vec_2({5, 6, 7, 8});
iterator_type it(vec_1.begin());
std::cout << " *it =" << *it << "; it.peek_next() = " << it.peek_next() << std::endl;
iterator_type_const cit(vec_2.begin());
std::cout << "*cit =" << *cit << "; cit.peek_next() = " << cit.peek_next() << std::endl;
return 0;
}
问题是要编译此示例,我必须允许非 const 版本的 peek_element 函数。否则编译器失败并显示消息
error: invalid initialization of reference of type 'std::iterator<std::forward_iterator_tag, int, long int, int*, int&>::reference {aka int&}' from expression of type 'const int'
据我所知,编译器使用 basic_functor::operator()
的非常量版本。
那么我该如何避免呢?是的,我知道 unary_function
已从 C++17 中删除。
最佳答案
好的,感谢评论中的讨论(@danadam 和@DavisHerring),我找到了答案。我误读了错误。编译器使用了正确版本的 basic_functor::operator()
好吧——问题是 iterator_functional::peek_next()
的返回类型。
我认为 const typename self::reference
与 const S&
相同,但事实并非如此。这不是对 const 的引用,而是 const 引用!因此,const
位被忽略,因此我试图将 basic_functor::operator()
返回的 const S&
转换为 S&
-- iterator_functional::peek_next()
的返回类型。
因此,解决方案不是使用const typename self::reference
,而是
inline const typename self::value_type& operator*() const
{ return m_functor(*m_it); }
相反。
谁知道!
附言在与@DavisHerring 进一步讨论后,我将改进后的工作示例放在这里。亮点:
basic_functor
不需要第二个 operator()
。只要是用const T
模板参数创建即可。S
模板参数并将其从 BaseIterator
中获取。请注意,我需要将 std::remove_reference
与 BaseIterator::reference
一起使用,因为 const_iterator::value_type
不是常量类型。<所以在这里(ta-da):
#include <functional>
#include <iostream>
#include <vector>
/**
* This is an empty functor which just returns back its argument.
*/
template <class T>
class basic_functor: public std::unary_function<T, T>
{
public:
typedef basic_functor self;
basic_functor()
{}
/**
* Returns back the argument passed to it.
*/
inline T& operator()(T& arg) const
{ return arg; }
};// class basic_functor
/**
* @param S type of the elements.
* @param BaseIterator type of the iterator it builds upon.
* @param functor which is applied to the element to which iterator points.
* by default it is empty functor -- the element itself is returned.
*/
template <typename BaseIterator,
typename Functor=basic_functor<
typename std::remove_reference<
typename BaseIterator::reference>::type > >
class iterator_functional:
public std::iterator<std::forward_iterator_tag,
typename Functor::result_type>
{
public:
///type defining itself
typedef iterator_functional self;
///type of the functor
typedef Functor functor_type;
///type of linear iterator
typedef BaseIterator base_iterator_type;
/**
* Constructor.
*/
iterator_functional(base_iterator_type it,
functor_type funct = functor_type()):
m_it(it),
m_functor(funct)
{
}
/**
* Copying constructor.
* @param other source of the data.
*/
iterator_functional(const self&other):
m_it(other.m_it),
m_functor(other.m_functor)
{ }
/**
* Provides access to element to which iterator points.
* @return element to which iterator points to.
*/
inline typename self::reference operator*() const
{ return m_functor(*m_it); }
/**
* Returns reference to the next element.
*/
inline const typename self::value_type& peek_next() const
{
auto temp_it = m_it;
++temp_it;
return
m_functor(*temp_it);
}//iterator_type_const
private:
base_iterator_type m_it;
functor_type m_functor;
};//iterator_functional;
typedef std::vector<int>::iterator v_int_iterator;
typedef std::vector<int>::const_iterator v_int_const_iterator;
typedef iterator_functional<v_int_iterator> iterator_type;
typedef iterator_functional<v_int_const_iterator> iterator_type_const;
int main()
{
std::vector<int> vec_1({1, 2, 3, 4});
const std::vector<int> vec_2({5, 6, 7, 8});
iterator_type it(vec_1.begin());
*it = 9;
std::cout << " *it =" << *it << "; it.peek_next() = " << it.peek_next() << std::endl;
iterator_type_const cit(vec_2.begin());
std::cout << "*cit =" << *cit << "; cit.peek_next() = " << cit.peek_next() << std::endl;
return 0;
}
关于c++ - 如何使用 C++ 中的平凡仿函数避免 "invalid initialization of reference"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221590/
我正在尝试将 keras.initializers 引入我的网络,following this link : import keras from keras.optimizers import RMS
我正在为程序创建某种前端。为了启动程序,我使用了调用 CreateProcess(),其中接收到一个指向 STARTUPINFO 结构的指针。初始化我曾经做过的结构: STARTUPINFO star
我已经模板化了 gray_code 类,该类旨在存储一些无符号整数,其基础位以格雷码顺序存储。这里是: template struct gray_code { static_assert(st
我已经查看了之前所有与此标题类似的问题,但我找不到解决方案。所有错误都表明我没有初始化 ArrayList。我是否没有像 = new ArrayList 这样初始化 ArrayList? ? impo
当涉及到 Swift 类时,我对必需的初始化器和委托(delegate)的初始化器有点混淆。 正如您在下面的示例代码中所见,NewDog 可以通过两种方式中的一种进行初始化。如您所见,您可以通过在初始
几天来我一直在为一段代码苦苦挣扎。我在运行代码时收到的错误消息是: 错误:数组初始值设定项必须是初始值设定项列表 accountStore(int size = 0):accts(大小){} 这里似乎
我想返回一个数组,因为它是否被覆盖并不重要,我的方法是这样的: double * kryds(double linje_1[], double linje_2[]){ double x = linje
尝试在 C++ 中创建一个简单的 vector 时,出现以下错误: Non-aggregates cannot be initialized with initializer list. 我使用的代码
如何在构造函数中(在堆栈上)存储初始化列表所需的临时状态? 例如,实现这个构造函数…… // configabstraction.h #include class ConfigAbstraction
我正在尝试编写一个 native Node 插件,它枚举 Windows 机器上的所有窗口并将它们的标题数组返回给 JS userland。 但是我被这个错误难住了: C:\Program Files
#include using namespace std; struct TDate { int day, month, year; void Readfromkb() {
我很难弄清楚这段代码为何有效。我不应该收到“数组初始值设定项必须是初始值设定项列表”错误吗? #include class B { public: B() { std::cout << "B C
std::map m = { {"Marc G.", 123}, {"Zulija N.", 456}, {"John D.", 369} }; 在 Xcode 中,我将 C+
为了帮助你明白这一点,我给出了我的代码:(main.cpp),只涉及一个文件。 #include #include using namespace std; class test{ public
这在 VS2018 中有效,但在 2008 中无效,我不确定如何修复它。 #include #include int main() { std::map myMap = {
我有一个类: #include class Object { std::shared_ptr object_ptr; public: Object() {} template
我正在为 POD、STL 和复合类型(如数组)开发小型(漂亮)打印机。在这样做的同时,我也在摆弄初始化列表并遇到以下声明 std::vector arr{ { 10, 11, 12 }, { 20,
我正在使用解析实现模型。 这是我的代码。 import Foundation import UIKit import Parse class User { var objectId : String
我正在观看 Java 内存模型视频演示,作者说与 Lazy Initialization 相比,使用 Static Lazy Initialization 更好,我不清楚他说的是什么想说。 我想接触社
如果您查看 Backbone.js 的源代码,您会看到此模式的多种用途: this.initialize.apply(this, arguments); 例如,这里: var Router =
我是一名优秀的程序员,十分优秀!