Can a successfully compileable template definition fail on instantiation?

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

    Can a successfully compileable template definition fail on instantiation?

    Assuming of course that the instantiation statement is 100% ok.
    As a matter of fact, that very instantiation passes successfuly on VC++ 7.1
    (and Borland) but since I was suspicious that it was OK there "more by luck
    than brain", I checked the same on Linux\GNU were the instantiation failed.

    This question is more for people who know well the C++ ISO standard, the
    chapter on templates. My hunch is that the problem is with the language spec.

    Thanks
  • Andrey Tarasevich

    #2
    Re: Can a successfully compileable template definition fail on instantiation?

    David wrote:[color=blue]
    > Assuming of course that the instantiation statement is 100% ok.
    > As a matter of fact, that very instantiation passes successfuly on[/color]
    VC++ 7.1[color=blue]
    > (and Borland) but since I was suspicious that it was OK there "more by[/color]
    luck[color=blue]
    > than brain", I checked the same on Linux\GNU were the instantiation[/color]
    failed.[color=blue]
    >
    > This question is more for people who know well the C++ ISO standard, the
    > chapter on templates. My hunch is that the problem is with the[/color]
    language spec.

    Why don't you just post some minimal piece of code that reproduces the
    problem?

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Michiel Salters

      #3
      Re: Can a successfully compileable template definition fail on instantiation?

      agpx1999@yahoo. com (David) wrote in message news:<b68ee409. 0405121825.5732 2a10@posting.go ogle.com>...[color=blue]
      > Assuming of course that the instantiation statement is 100% ok.
      > As a matter of fact, that very instantiation passes successfuly on VC++ 7.1
      > (and Borland) but since I was suspicious that it was OK there "more by luck
      > than brain", I checked the same on Linux\GNU were the instantiation failed.[/color]

      Yes, and this is in fact a very common technique. For instance,
      std::vector<T>: :vector( size_type, T const& = T(), {default allocator} )

      It is impossible to instantiate this member when only the size_type
      argument is provided and T doesn't have an accessible default ctor.

      Basically templates compile if there is at least one set of arguments
      for which they can be instantiated. The reverse is not true; some
      errors are too hard to detect without instantiating.

      E.g. a template with unsigned integer parameters A,B,C and N that
      could only compile if A^(N+3)+B^(N+3) ==C^(N+3). A compiler may
      reject this, because there is no set of arguments for which this holds
      but it isn't required to be aware of the proof to Fermats theorem.
      It may instead reject every attempt to instantiate.

      Regards,
      Michiel Salters

      Comment

      Working...