C++ 非成员操作员作为外部类的私有成员

C++ 非成员操作员作为外部类的私有成员,c++,class,operator-keyword,member,non-static,C++,Class,Operator Keyword,Member,Non Static,是否可能有一个非成员运算符,如 bool operator==(const std::string &l, const Token r) 作为无关类的私有成员函数解释器? 我用显而易见的方式尝试了一下,但没有效果(争论太多)。 我知道,“非成员函数[…]作为成员”的标题已经说明了相反的情况,但是有比函数更好的方法吗 bool isToken(const std::string &l, const Token r) 要进行依赖于解释器的(非静态)成员的比较 在解释器之外,无法将

是否可能有一个非成员运算符,如

bool operator==(const std::string &l, const Token r)
作为无关类的私有成员函数
解释器
? 我用显而易见的方式尝试了一下,但没有效果(争论太多)。 我知道,“非成员函数[…]作为成员”的标题已经说明了相反的情况,但是有比函数更好的方法吗

bool isToken(const std::string &l, const Token r)
要进行依赖于
解释器的(非静态)成员的比较

解释器
之外,无法将
标记
s与
字符串
s进行比较


一些进一步的信息:令牌是一个枚举,比较取决于构建
解释器时设置的语言

不可能按照您想要的方式制作
运算符==
的版本。它不能是静态的。如果它是一个成员,那么它必须接受一个参数

如果您愿意“复制代码”,那么就可以使用名称空间玩把戏。泛型解释器可以是一个模板,它将特定于语言的派生类作为模板参数。它又根据特定于语言的标记调用模板化的
操作符==

template <typename TOKEN>
bool operator == (const std::string &l, const TOKEN token) {
    return token == l;
}

// Language specific interpreters inherit from this template
template <typename LANG>
class Interpreter {
public:
    void interpret () {
        std::string s("hi");
        if (s == LANG::KEYWORD_ELSE) {}
    }
};
namespace Lang0 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}

namespace Lang1 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}
每个命名空间还为
操作符==
提供特定于语言的实现,以将字符串与特定于语言的标记进行比较

template <typename TOKEN>
bool operator == (const std::string &l, const TOKEN token) {
    return token == l;
}

// Language specific interpreters inherit from this template
template <typename LANG>
class Interpreter {
public:
    void interpret () {
        std::string s("hi");
        if (s == LANG::KEYWORD_ELSE) {}
    }
};
namespace Lang0 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}

namespace Lang1 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}

然后,当
解释器的模板实现
调用
操作符==
的模板版本时,它将解析为相应语言特定命名空间中的语言特定实现。

无法按照您想要的方式制作
操作符==
的版本。它不能是静态的。如果它是一个成员,那么它必须接受一个参数

如果您愿意“复制代码”,那么就可以使用名称空间玩把戏。泛型解释器可以是一个模板,它将特定于语言的派生类作为模板参数。它又根据特定于语言的标记调用模板化的
操作符==

template <typename TOKEN>
bool operator == (const std::string &l, const TOKEN token) {
    return token == l;
}

// Language specific interpreters inherit from this template
template <typename LANG>
class Interpreter {
public:
    void interpret () {
        std::string s("hi");
        if (s == LANG::KEYWORD_ELSE) {}
    }
};
namespace Lang0 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}

namespace Lang1 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}
每个命名空间还为
操作符==
提供特定于语言的实现,以将字符串与特定于语言的标记进行比较

template <typename TOKEN>
bool operator == (const std::string &l, const TOKEN token) {
    return token == l;
}

// Language specific interpreters inherit from this template
template <typename LANG>
class Interpreter {
public:
    void interpret () {
        std::string s("hi");
        if (s == LANG::KEYWORD_ELSE) {}
    }
};
namespace Lang0 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}

namespace Lang1 {
    bool operator == (const Interpreter::Token token, const std::string &l) {
        //...
    }
}

然后,当
解释器的模板实现
调用
操作符==
的模板版本时,它将解析为相应特定于语言的命名空间中的特定于语言的实现。

因为比较是由解释器的非静态成员进行的(例如,is_equal()),进行比较需要三个对象:解释器、字符串和令牌

如果在比较时知道只有一个有效的解释器对象实例,则可以静态访问该实例,例如

bool operator==(const std::string &l, const Token r)
{
   return Interpreter::instance().is_equal(l, r);
}

其中static member instance()返回执行工作的解释器对象。

由于比较是由解释器的非静态成员进行的(例如,is_equal()),因此需要三个对象进行比较:解释器、字符串和令牌

如果在比较时知道只有一个有效的解释器对象实例,则可以静态访问该实例,例如

bool operator==(const std::string &l, const Token r)
{
   return Interpreter::instance().is_equal(l, r);
}


其中,static member instance()返回执行此操作的解释器对象。

什么是
令牌
,如果
令牌
是LHS,那么
==
将如何工作?令牌始终是类似枚举的关键字或条件。关键字_ELSE==“ELSE”对于“english”解释器应为true,关键字_ELSE==“sonst”对于“german”解释器应为true。另外,如果==运算符是在Intepreter中定义的,您认为语法是什么样的?或者您正在请求一个只在解释器上下文中工作的运算符,即解释器的成员方法?您是否可以将字符串转换为令牌(在解释器上下文中),而不是将令牌与字符串进行比较然后将生成的标记相互比较?您的程序是否需要动态绑定到解释器,或者您是否可以假设您的程序在其持续时间内以单一语言模式运行?什么是
标记
,如果
Token
是LHS,那么
==
将如何工作?Token是一个类似枚举的关键字\u ELSE或CONDITION\u。关键字_ELSE==“ELSE”对于“english”解释器应为true,关键字_ELSE==“sonst”对于“german”解释器应为true。另外,如果==运算符是在Intepreter中定义的,您认为语法是什么样的?或者您正在请求一个只在解释器上下文中工作的运算符,即解释器的成员方法?您是否可以将字符串转换为令牌(在解释器上下文中),而不是将令牌与字符串进行比较然后将生成的标记相互比较?您的程序是否需要动态绑定到解释器,或者您是否可以假设您的程序在其持续时间内以单一语言模式运行?遗憾的是,代码复制也无法工作。解释器将带有翻译的给定数据库加载到映射中。为什么C++不允许这个问题,或者它被遗忘了吗?我不确定如何回答你的问题。我的印象是,由于您希望将
操作符==
作为
解释器的成员,因此比较只能在
解释器
实例的上下文中进行。这就是为什么我认为上面的解决方案适合你。我假设英语解释器与Deutsch Intepreter是不同的实例。支持的语言在编译时是未知的。我可能会让用户定义自定义语言,并让他为自己的程序选择它。类似于解释器tab1('user1.lang')我的解决方案仍然有效。名称空间可以基于语言命名语言索引(
Lang0
Lang1
)。只要您愿意限制语言的数量,这就行了