boost::enable_if enabler() 与使用 boost::iterator_facade 创建迭代器的教程相关
boost::enable_if enabler() related to the tutorial on creating an iterator with boost::iterator_facade
所以...查看此代码片段:
#include <boost/iterator/iterator_facade.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/utility/enable_if.hpp>
template <class Value>
class LMI
: public boost::iterator_facade<LMI<Value>, Value, boost::forward_traversal_tag>
{
private:
int xsize, ysize, x, y;
Level *level {0}; // iterator invalid if level == 0
public:
LMI(LMI<OtherValue> const &L,
typename boost::enable_if<boost::is_convertible<OtherValue*,Value*>,
enabler>::type = enabler())
: xsize {L.xsize}, ysize {L.ysize}, x {L.x}, y {L.y}, level {L.level}
{};
};
编译器抱怨没有标识符 'enabler' ...这一切都来自 https://www.boost.org/doc/libs/1_72_0/libs/iterator/doc/iterator_facade.html#telling-the-truth
的 boost 示例文档
我意识到 enable_if 和 is_convertible 是神奇的模板,可以确定是否创建了这个转换构造函数(教程中说的很多),但我不知道看不到关于此“enabler()”函数的任何建议 --- 教程中的这种用法不正确吗?自教程编写以来,用法是否发生了变化?
可能会帮助读者知道这个模板是这样使用的:
typedef LMI<Tile> Level_Map_Iterator;
typedef LMI<Tile const> cLevel_Map_Iterator;
...关键是在创建 LMI 时它会创建转换构造函数而在创建 LMI 时它不会。
所以...不好意思,如果我添加
struct enabler {};
到class模板的私有部分,一切都很好。没有确切地解释这是如何工作的——但我现在对这个秘诀很满意。显然,我是在根据自己的作品改编教程示例,唉。
所以...查看此代码片段:
#include <boost/iterator/iterator_facade.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/utility/enable_if.hpp>
template <class Value>
class LMI
: public boost::iterator_facade<LMI<Value>, Value, boost::forward_traversal_tag>
{
private:
int xsize, ysize, x, y;
Level *level {0}; // iterator invalid if level == 0
public:
LMI(LMI<OtherValue> const &L,
typename boost::enable_if<boost::is_convertible<OtherValue*,Value*>,
enabler>::type = enabler())
: xsize {L.xsize}, ysize {L.ysize}, x {L.x}, y {L.y}, level {L.level}
{};
};
编译器抱怨没有标识符 'enabler' ...这一切都来自 https://www.boost.org/doc/libs/1_72_0/libs/iterator/doc/iterator_facade.html#telling-the-truth
的 boost 示例文档我意识到 enable_if 和 is_convertible 是神奇的模板,可以确定是否创建了这个转换构造函数(教程中说的很多),但我不知道看不到关于此“enabler()”函数的任何建议 --- 教程中的这种用法不正确吗?自教程编写以来,用法是否发生了变化?
可能会帮助读者知道这个模板是这样使用的:
typedef LMI<Tile> Level_Map_Iterator;
typedef LMI<Tile const> cLevel_Map_Iterator;
...关键是在创建 LMI 时它会创建转换构造函数而在创建 LMI 时它不会。
所以...不好意思,如果我添加
struct enabler {};
到class模板的私有部分,一切都很好。没有确切地解释这是如何工作的——但我现在对这个秘诀很满意。显然,我是在根据自己的作品改编教程示例,唉。