Template and default values of non-template pointers arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clyde

    Template and default values of non-template pointers arguments

    Hi,

    what i'm trying to do is:

    /////////////// Code Start

    template <class TType, int* p = 0>
    class Template
    {
    public:
    Template<TType, p>() {};

    };

    /////////////// Code End

    That works on Visual Studio 2005, but doesn't work on mingw 5.1.4 and
    comeau.

    They both says:

    mingw:
    could not convert template argument `0' to `int*

    comeau:
    argument of type "int" is incompatible with template parameter of type
    "int *"
    template <class TType, int* p = 0>
    ^

    so the problem is quite clear, it seems there's no an acceptable
    conversion from int to int*

    But if i try to do something like that:

    /////////////// Code Start

    #define zero (int*)0

    template <class TType, int* p = zero>
    class Template
    {
    public:
    Template<TType, p>() {};

    };

    /////////////// Code End


    Now, that works on comeau, but still doesn't work with mingw.


    My questions:
    1] Why is that illegal?
    2] How can solve this problem?


    Many thanks,

    --

    Clyde
  • Victor Bazarov

    #2
    Re: Template and default values of non-template pointers arguments

    Clyde wrote:
    what i'm trying to do is:
    >
    /////////////// Code Start
    >
    template <class TType, int* p = 0>
    class Template
    {
    public:
    Template<TType, p>() {};
    >
    };
    >
    /////////////// Code End
    No, that's not what you're trying to do. That's *how* you're trying to
    do what you think you need. You should consider explaining *what* you
    think you need the null pointer for. There can be no compile-time check
    for a null pointer...
    >
    That works on Visual Studio 2005, but doesn't work on mingw 5.1.4 and
    comeau.
    It must be an extension offered by VC++.
    >
    They both says:
    >
    mingw:
    could not convert template argument `0' to `int*
    >
    comeau:
    argument of type "int" is incompatible with template parameter of type
    "int *"
    template <class TType, int* p = 0>
    ^
    >
    so the problem is quite clear, it seems there's no an acceptable
    conversion from int to int*
    >
    But if i try to do something like that:
    >
    /////////////// Code Start
    >
    #define zero (int*)0
    >
    template <class TType, int* p = zero>
    class Template
    {
    public:
    Template<TType, p>() {};
    >
    };
    >
    /////////////// Code End
    >
    >
    Now, that works on comeau, but still doesn't work with mingw.
    >
    >
    My questions:
    1] Why is that illegal?
    Here we just say "because the Standard says so". 14.3.2/5 prohibits '0'
    (the integer literal) to be used as the non-type template *argument* for
    a template *parameter* of type 'pointer to T'. The conversions do not
    apply.

    If you need the real rationale, you'll have to to ask in 'comp.std.c++'
    when it's operational.
    2] How can solve this problem?
    Solve? Not sure what you mean. The requirement is that the pointer
    non-type template argument to be the address of a real object, with
    external linkage. A null pointer does not satisfy that requirement.
    Now, if you do have a problem (that you're trying to solve by writing
    your template with the wrong default argument), you haven't stated it
    yet, so we can't really tell you how to solve it. Please explain what
    you're trying to accomplish.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • James Kanze

      #3
      Re: Template and default values of non-template pointers arguments

      On Oct 9, 1:00 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
      Clyde wrote:
      what i'm trying to do is:
      /////////////// Code Start
      template <class TType, int* p = 0>
      class Template
      {
      public:
      Template<TType, p>() {};
      };
      /////////////// Code End
      No, that's not what you're trying to do. That's *how* you're
      trying to do what you think you need. You should consider
      explaining *what* you think you need the null pointer for.
      There can be no compile-time check for a null pointer...
      Surely you've heard of partial specialization. Or template
      meta-programming (things like boost::enable_i f). Or perhaps he
      doesn't need a compile-time check; just somewhere in the code,
      he has "if ( p != NULL ) ...". (Of course, since p is in fact a
      constant in any particular instantiation, the compiler can
      evaluate the if at compile time, and not generate the body if
      the pointer is null. However...)
      That works on Visual Studio 2005, but doesn't work on mingw
      5.1.4 and comeau.
      It must be an extension offered by VC++.
      Note that there are really two separate issues here. First is
      the fact that the standard does not apply the null pointer
      constant conversion, so 0 can only be used to instantiate a
      template argument of integral type. The second is that the
      standard doesn't allow a null pointer, period, as the argument
      to a non-type template parameter of pointer type---the argument
      must be "the address of an object or function with external
      linkage, including function templates and function
      template-ids[...]". A null pointer is NOT the address of an
      object, so it's not allowed.

      The next release of the standard will allow null pointers,
      although it won't allow the use of 0 for them; the argument
      would have to be either (int*)0 or nullptr.
      They both says:
      mingw:
      could not convert template argument `0' to `int*
      comeau:
      argument of type "int" is incompatible with template
      parameter of type "int *"
      template <class TType, int* p = 0>
      so the problem is quite clear, it seems there's no an
      acceptable conversion from int to int*
      There is no implicit conversion from int to int*, ever. The
      conversion in question here is the "null pointer constant"
      conversion: a null pointer constant (an constant integral
      expression evaluating to zero) will convert implicitly to a
      pointer in some specific contexts. According to the standard,
      this is not one of them.
      But if i try to do something like that:
      /////////////// Code Start
      #define zero (int*)0
      template <class TType, int* p = zero>
      class Template
      {
      public:
      Template<TType, p>() {};
      };
      /////////////// Code End
      Now, that works on comeau, but still doesn't work with mingw.
      It's illegal, at least according to C++03. It will be legal in
      the next version of the standard.
      My questions:
      1] Why is that illegal?
      Here we just say "because the Standard says so". 14.3.2/5
      prohibits '0' (the integer literal) to be used as the non-type
      template *argument* for a template *parameter* of type
      'pointer to T'. The conversions do not apply.
      If you need the real rationale, you'll have to to ask in
      'comp.std.c++' when it's operational.
      Historically, *no* implicit conversions were allowed for
      non-type template arguments; IIRC, the first implementation of
      templates I used wouldn't accept 0 for a long argument. (It had
      to be 0L. And I'll admit that this was a long time ago, and my
      memory isn't that sure.) Basically, templates were something
      new, and without any experience, it wasn't too clear what the
      implications of conversions might be. In the standard, integral
      promotions and integral conversions are allowed (so 0 can be
      used for an argument of type long), but the only conversions
      allowed for a pointer argument are array to pointer (so you can
      use the name of an array) and adding cv qualifications (so you
      can pass the address of a non-const object to a pointer to
      const). Not even derived to base is applied (implicitly).

      The next version of the standard will loosen this requirement
      somewhat, and allow conversions of std::nullptr_t to a pointer
      type.
      2] How can solve this problem?
      Solve? Not sure what you mean. The requirement is that the
      pointer non-type template argument to be the address of a real
      object, with external linkage. A null pointer does not
      satisfy that requirement.
      Yes, but that requirement will be loosened. In the meantime...
      the solution to every problem is an additional level of
      indirection, as they used to say. He can definitely do
      something like:

      extern int *const null = 0 ;

      template< typename TType, int *const& p = null >
      ...

      Of course, every time he wants to instantiate the template,
      he'll have to introduce an additional variable ,so the cure may
      be worse than the disease. (Note too that the "extern" is
      necessary in the above. By default, const objects have internal
      linkage, and a template can only be instantiated over objects
      with external linkage.) Alternatively, he just provides a dummy
      int, and uses its address as the default; within the template,
      he can check if the argument is equal to the dummy address.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...