attempting template metaprog to print arbitrary container

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tutufan@gmail.com

    attempting template metaprog to print arbitrary container

    Hi,

    I'm trying to use template metaprogramming to print (for debugging
    purposes) the contents of an arbitrary (random-access) container. So,
    for example, I'd like to be able to print a vector<int>, a vector<
    vector<double> >, etc., all from the same code.

    It seems like this is close to a solution, but the g++ doesn't like it
    (error at bottom). Any suggestions?

    template<typena me T, unsigned Dim>
    struct debug_print_con tainer {

    template<unsign ed Dim0>
    struct debug_print_con tainer_0 {
    void print(typename T::value_type item, const char *label, ostream
    &out) {
    debug_print_con tainer<typename
    T::value_type,D im-1>::debug_print (item, label, out);
    }
    };

    template<>
    struct debug_print_con tainer_0<1> {
    void print(typename T::value_type item, const char *label, ostream
    &out) {
    out << item;
    }
    };

    static void
    debug_print(T cont, const char *label=NULL, ostream &out=cout,
    const char *terminator="\n ") {
    if (label)
    out << label << ": ";
    out << "[";
    for (typename T::size_type i=0; i<cont.size(); i++) {
    debug_print_con tainer_0<Dim>:: print(cont[i], label, out);
    if (i < cont.size() - 1)
    out << ", ";
    }
    out << "]" << terminator;
    }
    };


    1094: error: explicit specialization in non-namespace scope 'struct
    debug_print_con tainer<T, Dim>'
    1094: error: enclosing class templates are not explicitly specialized
    1095: error: template parameters not used in partial specialization:
    1095: error: 'T'
    1095: error: 'Dim'

  • Alan Johnson

    #2
    Re: attempting template metaprog to print arbitrary container

    tutufan@gmail.c om wrote:[color=blue]
    > It seems like this is close to a solution, but the g++ doesn't like it
    > (error at bottom). Any suggestions?[/color]
    [snip][color=blue]
    > 1094: error: explicit specialization in non-namespace scope 'struct
    > debug_print_con tainer<T, Dim>'
    > 1094: error: enclosing class templates are not explicitly specialized
    > 1095: error: template parameters not used in partial specialization:
    > 1095: error: 'T'
    > 1095: error: 'Dim'
    >[/color]

    You are trying to explicitly specialize a member template without
    explicitly specializing its enclosing templates, which is not allowed.
    See 14.7.3.18 of the standard:

    "In an explicit specialization declaration for a member of a class
    template or a member template that appears in namespace scope, the
    member template and some of its enclosing class templates may remain
    unspecialized, except that the declaration shall not explicitly
    specialize a class member template if its enclosing class templates are
    not explicitly specialized as well."

    There is probably a good reason why such a thing is not allowed, but I
    don't know it.

    --
    Alan Johnson

    Comment

    • tutufan@gmail.com

      #3
      Re: attempting template metaprog to print arbitrary container

      That's kind of what I thought. It seems like the problem would be
      solved if I could just explicitly specialize the outer template, but I
      don't see how to do that. I tried doing something like

      template<>
      struct debug_print_con tainer<T,Dim>:: debug_print_con tainer_0<1> {

      but it didn't help. Any thoughts?

      This seems like the kind of function that'd be generally useful, so I
      wonder if someone hasn't already done it.

      Comment

      Working...