C++ 模板向导的难题

C++ 模板向导的难题,c++,templates,C++,Templates,我想做以下几点: const int someInt; const std::vector<int> someIntList; const std::vector<std::vector<int>> someNestedIntList; Marshall(someInt); // trivial case Marshall(someIntList); // difficult case Marshall(someNestedIntList); // diff

我想做以下几点:

const int someInt;
const std::vector<int> someIntList;
const std::vector<std::vector<int>> someNestedIntList;

Marshall(someInt); // trivial case
Marshall(someIntList); // difficult case
Marshall(someNestedIntList); // difficult case
template<std::vector<class Element>> 
void Marshall(const std::vector<Element>& toBeMarshalled)
{
    for (int i=0; i<toBeMarshalled.size(); ++i)
        Marshall<Element>(toBeMarshalled[i]);
}
const int-someInt;
const std::vector someIntList;
const std::vector someNestedIntList;
马歇尔(某某);//琐事
马歇尔(某些国际组织);//疑难案件
Marshall(某个嵌套列表);//疑难案件
我尝试了以下方法:

const int someInt;
const std::vector<int> someIntList;
const std::vector<std::vector<int>> someNestedIntList;

Marshall(someInt); // trivial case
Marshall(someIntList); // difficult case
Marshall(someNestedIntList); // difficult case
template<std::vector<class Element>> 
void Marshall(const std::vector<Element>& toBeMarshalled)
{
    for (int i=0; i<toBeMarshalled.size(); ++i)
        Marshall<Element>(toBeMarshalled[i]);
}
模板
void Marshall(const std::vector&toBeMarshalled)
{

对于(int i=0;i,粘贴的代码包含模板声明结束时的>>/Cuth.C++编译器将解释为两个闭合角括号,而不是一个右移位操作符。


尝试<代码>模板>代码>,在括号之间有一个空间。

在粘贴模板声明结束时粘贴的代码包含“代码> > >代码>。C++编译器将解释这不是两个闭合角括号,而是作为一个单右移运算符。


请尝试使用
template
,括号之间留有空格。

模板声明错误。请执行以下操作:

template< class Element >
void marshall( std::vector< Element > const& v )
模板
void marshall(标准::向量<元素>常数和v)

干杯,

模板声明错误。请执行以下操作:

template< class Element >
void marshall( std::vector< Element > const& v )
模板
void marshall(标准::向量<元素>常数和v)

干杯&hth.,

您的模板有两个问题:

  • 模板声明错误。此处仅列出模板参数,而不列出函数参数类型。此外,
    >
    被解析为移位运算符
  • std::vector
    有两个模板参数。虽然在日常工作中很少使用第二个,但它仍然存在,应该列出,否则如果有人试图将它与不使用默认分配器的
    std::vector
    一起使用,模板将失败
  • 这适用于所有
    std::vector
    实例:

    template< typename T > 
    void Marshall(const T& toBeMarshalled)
    {
      // ...
    }
    
    template< typename T, class A > 
    void Marshall(const std::vector<T,A>& toBeMarshalled)
    {
        for (typename std::vector<T,A>::size_type i=0; i<toBeMarshalled.size(); ++i)
            Marshall(toBeMarshalled[i]);
    }
    
    模板
    无效马歇尔(const T&toBeMarshalled)
    {
    // ...
    }
    模板<类型名T,A类>
    void Marshall(const std::vector&toBeMarshalled)
    {
    
    对于(typename std::vector::size_type i=0;i模板有两个问题:

  • 模板声明错误。此处仅列出模板参数,而不列出函数参数类型。此外,
    >
    被解析为移位运算符
  • std::vector
    有两个模板参数。虽然在日常工作中很少使用第二个,但它仍然存在,应该列出,否则如果有人试图将它与不使用默认分配器的
    std::vector
    一起使用,模板将失败
  • 这适用于所有
    std::vector
    实例:

    template< typename T > 
    void Marshall(const T& toBeMarshalled)
    {
      // ...
    }
    
    template< typename T, class A > 
    void Marshall(const std::vector<T,A>& toBeMarshalled)
    {
        for (typename std::vector<T,A>::size_type i=0; i<toBeMarshalled.size(); ++i)
            Marshall(toBeMarshalled[i]);
    }
    
    模板
    无效马歇尔(const T&toBeMarshalled)
    {
    // ...
    }
    模板<类型名T,A类>
    void Marshall(const std::vector&toBeMarshalled)
    {
    对于(typename std::vector::size_type i=0;i我可以建议一些boost元编程吗

    #include <boost/type_traits.hpp>
    #include <boost/utility/enable_if.hpp>
    
    template <class T>
    struct has_begin_end
    {
        template <class U,
                  typename U::const_iterator (U::*)() const,
                  typename U::const_iterator (U::*)() const>
        struct sfinae { };
    
        template <class U>
        static char test(sfinae<U, &U::begin, &U::end>*);
    
        template <class>
        static long test(...);
    
        enum { value = (1 == sizeof test<T>(0)) };
        typedef boost::integral_constant<bool, value> type;
    };
    
    template <class Value>
    typename boost::disable_if<has_begin_end<Value>, void>::type
    Marshall(const Value& value)
    {
        std::cout << value << ' ';
    }
    
    template <class Container>
    typename boost::enable_if<has_begin_end<Container>, void>::type
    Marshall(const Container& c)
    {
        std::for_each(c.begin(), c.end(), Marshall<typename Container::value_type>);
    }
    
    int main()
    {
        const int someInt = 42;
        const std::vector<int> someIntList {2, 3, 5, 7};
        const std::vector<std::vector<int>> someNestedIntList {{11, 13}, {17, 19}};
    
        Marshall(someInt);
        Marshall(someIntList);
        Marshall(someNestedIntList);
    }
    
    #包括
    #包括
    模板
    结构具有\u开始\u结束
    {
    模板
    结构sfinae{};
    模板
    静态炭试验(sfinae*);
    模板
    静态长试验(…);
    枚举{value=(1==sizeof test(0))};
    typedef boost::积分_常量类型;
    };
    模板
    typename boost::禁用_if::type
    马歇尔(常量值和值)
    {
    我可以提出一些建议和促进元编程吗

    #include <boost/type_traits.hpp>
    #include <boost/utility/enable_if.hpp>
    
    template <class T>
    struct has_begin_end
    {
        template <class U,
                  typename U::const_iterator (U::*)() const,
                  typename U::const_iterator (U::*)() const>
        struct sfinae { };
    
        template <class U>
        static char test(sfinae<U, &U::begin, &U::end>*);
    
        template <class>
        static long test(...);
    
        enum { value = (1 == sizeof test<T>(0)) };
        typedef boost::integral_constant<bool, value> type;
    };
    
    template <class Value>
    typename boost::disable_if<has_begin_end<Value>, void>::type
    Marshall(const Value& value)
    {
        std::cout << value << ' ';
    }
    
    template <class Container>
    typename boost::enable_if<has_begin_end<Container>, void>::type
    Marshall(const Container& c)
    {
        std::for_each(c.begin(), c.end(), Marshall<typename Container::value_type>);
    }
    
    int main()
    {
        const int someInt = 42;
        const std::vector<int> someIntList {2, 3, 5, 7};
        const std::vector<std::vector<int>> someNestedIntList {{11, 13}, {17, 19}};
    
        Marshall(someInt);
        Marshall(someIntList);
        Marshall(someNestedIntList);
    }
    
    #包括
    #包括
    模板
    结构具有\u开始\u结束
    {
    模板
    结构sfinae{};
    模板
    静态炭试验(sfinae*);
    模板
    静态长试验(…);
    枚举{value=(1==sizeof test(0))};
    typedef boost::积分_常量类型;
    };
    模板
    typename boost::禁用_if::type
    马歇尔(常量值和值)
    {
    
    std::cout然而,如果出现一个使用不同分配器的
    std::vector
    实例,则此操作将失败。@sbi:好的,谢谢!我甚至没有考虑自定义分配器。Otoh.,我不建议浪费时间添加此类支持,除非在库代码(如Boost)中,您必须支持所有类型的客户端德:-)干杯,好吧,在一个内部库中,我看到很多代码在某人突然需要自定义分配器时失败。这不是我的代码,但我仍然花了很多时间与需要支持的人一起解决出现的所有问题。由于这些疏忽,无法编译的东西是最容易遇到的问题。Muc更糟糕的是,当某些东西编译时,却悄悄地调用了错误的重载函数模板。这会让调试变得有趣。但是,如果出现一个使用不同分配器的
    std::vector
    实例,这会失败。@sbi:对,谢谢!我甚至没有想到自定义分配器。Otoh.,我不会重新编译不要浪费时间添加这种支持,除非在库代码(如Boost)中,您必须支持各种客户端代码。:-)干杯,好吧,在一个内部库中,我看到很多代码在某人突然需要自定义分配器时失败。这不是我的代码,但我仍然花了很多时间与需要支持的人一起解决出现的所有问题。由于这些疏忽,无法编译的东西是最容易遇到的问题。Muc更糟糕的是,当某些东西编译时,却悄悄地调用了错误的重载函数模板。这会使调试变得有趣。在
    size\u type
    之前需要
    typename
    (尽管最好使用迭代器)。基本
    Marshall
    函数不一定需要模板化(如果只对特定类型有效,则可能不需要)。在
    size\u type
    之前需要
    typename
    (尽管最好使用迭代器)。基本
    Marshall
    函数不一定需要模板化(如果它只对特定类型有效,则可能不需要模板化).Wow。这将适用于具有
    begin()
    end()的任何类似范围的类型