是否可以使用模板来识别两种不同的类型?

时间:2021-11-28 06:59:01

I've got the following code. I have to define the operator() for all types available in MyVariant (bool, int, string, const char*). However, since StartsWith is only applicable to type string, all other functors should return false.

我有以下代码。我必须为MyVariant中可用的所有类型定义operator()(bool,int,string,const char *)。但是,由于StartsWith仅适用于类型字符串,因此所有其他仿函数都应返回false。

#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

typedef variant<bool, int, string, const char*> MyVariant;

class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(string &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    bool operator()(int &other) const
    {
        return false;
    }
    bool operator()(bool &other) const
    {
        return false;
    }
    bool operator()(const char* other) const
    {
        return false;
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

int main(int argc, char **argv) 
{
    MyVariant s1 = "hello world!";
    if(apply_visitor(StartsWith("hel"), s1))
    {
        cout << "starts with" << endl;
    }
    return 0;
}

The above code works fine. But in order to make it more concise, I thought it might be possible to use templates to have one functor for string, and one for other types. I tried the following, but the result is that the second functor is always the one invoked.

上面的代码工作正常。但为了使其更简洁,我认为可以使用模板为字符串设置一个仿函数,为其他类型设置一个仿函数。我尝试了以下方法,但结果是第二个仿函数始终是调用的。

template<typename T>
class StartsWith
    : public boost::static_visitor<bool>
{
public:
    T mPrefix;
    bool operator()(T &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    template<typename U>
    bool operator()(U &other)const
    {
        return false;
    }
    StartsWith(T const& prefix):mPrefix(prefix){}
};

The following code did not work either:

以下代码也不起作用:

class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(string &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    template<typename U>
    bool operator()(U &other)const
    {
        return false;
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

Is there anyway I can avoid multiple "return false" statements for types other than string?

反正我是否可以为字符串以外的类型避免多个“return false”语句?

3 个解决方案

#1


2  

bool operator()(std::string const& other ) const {...}
template< class T >
typename boost::disable_if<boost::is_same<T, std::string>, bool >::type
operator()( T const& ) const {return false;}

#2


1  

This one works for me:

这个对我有用:

class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(const string &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    template<typename U>
    bool operator()(U other)const
    {
        return false;
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

Off topic: std::string::compare() returns int.

关闭主题:std :: string :: compare()返回int。

#3


1  

The problem was that I was using const char* in my variant. Changing the following line:

问题是我在我的变体中使用const char *。更改以下行:

MyVariant s1 = "hello world!";

to

MyVariant s1 = string("hello world!");

solved the problem and got the template version working.

解决了问题并使模板版本正常工作。

#1


2  

bool operator()(std::string const& other ) const {...}
template< class T >
typename boost::disable_if<boost::is_same<T, std::string>, bool >::type
operator()( T const& ) const {return false;}

#2


1  

This one works for me:

这个对我有用:

class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(const string &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    template<typename U>
    bool operator()(U other)const
    {
        return false;
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

Off topic: std::string::compare() returns int.

关闭主题:std :: string :: compare()返回int。

#3


1  

The problem was that I was using const char* in my variant. Changing the following line:

问题是我在我的变体中使用const char *。更改以下行:

MyVariant s1 = "hello world!";

to

MyVariant s1 = string("hello world!");

solved the problem and got the template version working.

解决了问题并使模板版本正常工作。