C ++模板函数问题

时间:2011-01-02 18:05:20

标签: c++ templates

我正在尝试编写一个转换函数,它将值和一系列查找表作为参数。查找表具有以下声明:

template< typename fromType, typename toType> struct DataTranslator;

我可以使用以下翻译模板函数声明:

template< typename Return, typename Direction, typename ValType, typename TransType>
Return translate(ValType val, TransType& trans);

template< typename Return, typename Direction, typename ValType, typename TransType, typename... OtherTrans>
Return translate(ValType val, TransType& trans, OtherTrans&... others);

然后,我可以执行以下操作:

DataTranslator<specificBaud, universalBaud>::type baudTranslator;
DataTranslator<universalBaud, std::string>::type baudCmdTranslator;

specificBaud myBaud;
....
std::string result = translate<std::string, forward_trans>(myBaud, baudTranslator, baudCmdTranslator);

但如果我将翻译函数的声明更改为:

template< typename Return, typename Direction, typename ValType, typename FT, typename TT>
Return translate(ValType val, typename DataTranslator<FT, TT>::type& trans);

template< typename Return, typename Direction, typename ValType, typename FT, typename TT, typename... OtherTrans>
Return translate(ValType val, typename DataTranslator<FT, TT>::type& trans, OtherTrans&... others);

进行翻译调用时,我收到一个无匹配的函数调用错误。我正在使用GCC 4.5为我的编译器使用-std = c ++ 0x标志。

我的主要问题是为什么第二个版本不起作用?我已经看到了一些示例(主要是在varidic模板的一个提议中提出的Tuple类),他们在那里进行了类型的分解以专门化它。

完整的测试代码:

/*
* bimap_test.cpp
*  
*  Created on: Dec 27, 2010
*      Author: natejohn
*/

#include <iostream>
#include <utility>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/tag.hpp>
#include <boost/mpl/if.hpp>
#include <boost/function.hpp>
#include <tr1/type_traits>

enum silly {car, boat, plane, truck};
enum furry {bunny, dog, cat, donkey, critter};

struct forward_trans {};
struct reverse_trans {};

template<typename from, typename to> struct DataTranslator
{
    typedef std::pair<from, to> Element;
    typedef boost::multi_index_container<Element, boost::multi_index::indexed_by<
            boost::multi_index::ordered_unique<
            boost::multi_index::tag<forward_trans>,
            boost::multi_index::member<Element, typename Element::first_type,     &Element::first>
            >,
            boost::multi_index::ordered_unique<
        boost::multi_index::tag<reverse_trans>,
        boost::multi_index::member<Element, typename Element::second_type, &Element::second>
        >
        >
> type;

     // Added after Answer accepted as what I fixed to get this to work
     type trans;


};

template<typename R, typename I> struct forward_extractor
{
    R operator()(I it) { return it->second; }
};

template<typename R, typename I> struct reverse_extractor
{
    R operator()(I it) { return it->first; }
};

template<typename from, typename to> struct FunctionTranslator
{
    typedef boost::function<to (from) > forward_type;
    typedef boost::function<from (to) > reverse_type;
};

/*template<typename R, typename D, typename V, typename Trans>
R translate(V v, Trans& t);

template<typename R, typename D, typename V, typename Trans, typename... Others>
R translate(V v, Trans& t, Others&... o);
*/

template<typename R, typename D, typename V, typename FT, typename TT, typename...     others>
R translate(V val, boost::function<TT(FT)> trans, others&... o)
{
    TT temp = trans(val);
     return static_cast<R>(translate<R, D>(temp, o...));
}

template<typename R, typename D, typename V, typename FT, typename TT>
R translate(V val, boost::function<TT(FT)>& func)
{
    return static_cast<R>(func(val));
}

template<typename R, typename D, typename V, typename FT, typename TT>
R translate(V val, typename DataTranslator<FT, TT>::type& trans)
{
    typedef typename DataTranslator<FT, TT>::type::template index<D>::type lookup_table;
    typedef typename lookup_table::iterator lut_iterator;
    lookup_table& lut = boost::multi_index::get<D>(trans);

R not_found;

typedef typename boost::mpl::if_c<std::tr1::is_same<D, forward_trans>::value, forward_extractor<TT, lut_iterator>,
            typename boost::mpl::if_c<std::tr1::is_same<D, reverse_trans>::value, reverse_extractor<FT, lut_iterator>, bool>::type>::type extractor_type;

extractor_type ext;

lut_iterator it = lut.find(val);

if(it == lut.end())
{
    return not_found;
}

return static_cast<R>(ext(it));

}


template<typename R, typename D, typename V, typename FT, typename TT, typename... others>
R translate(V val, typename DataTranslator<FT, TT>::type& trans, others&... o)
{
typedef typename DataTranslator<FT, TT>::type::template index<D>::type lookup_table;
typedef typename lookup_table::iterator lut_iterator;
lookup_table& lut = boost::multi_index::get<D>(trans);
R not_found;
typedef typename boost::mpl::if_c<std::tr1::is_same<D, forward_trans>::value, forward_extractor<TT, lut_iterator>,
            typename boost::mpl::if_c<std::tr1::is_same<D, reverse_trans>::value, reverse_extractor<FT, lut_iterator>, bool>::type >::type extractor_type;
extractor_type ext;


lut_iterator it = lut.find(val);

if(it == lut.end())
{
    return not_found;
}

return translate<R, D>(ext(it), o...);

}


int main(int argc, char** argv)
{
typedef typename DataTranslator<silly, furry>::type SillyToFurryTranslatorType;
typedef typename DataTranslator<furry, std::string>::type FurryToStringTranslatorType;


DataTranslator<silly, furry>::type stft;
DataTranslator<furry, std::string>::type ftst;

stft.insert(std::make_pair(car, dog));
stft.insert(std::make_pair(boat, cat));
ftst.insert(std::make_pair(dog, "Doggie!"));
ftst.insert(std::make_pair(cat, "Earl the Dead Cat"));

std::string result = translate<furry, forward_trans>(car, stft, ftst);
std::cout << result << std::endl;

return 0;
}

1 个答案:

答案 0 :(得分:4)

我认为问题是如果你试图从嵌套在里面的某种类型的类型中推断出外部类的类型,那么C ++模板参数推导会被破坏。例如,这将无法正常工作:

template <typename T> void DoSomething(typename std::vector<T>::iterator itr);

std::vector<int> v;
DoSomething(v.begin());

这样做的原因是编译器无法明确地确定外部类型在所有情况下必须是什么。例如,考虑这种情况:

template <typename T> class Pathological {
      typedef int type;
};

template <typename T> void WhatIsT(typename Pathological<T>::type huh);

这里的问题是Pathological的每个实例都具有为其类型类型指定的相同类型。因此,如果您传入Pathological<int>::typePathological<double>::type,则编译器无法确定外部类型是什么,因为这两种类型都评估为int。

希望这有帮助!