Topic: template class and extern "C


Author: Bjorn Fahller <Bjorn.Fahller@ebc.ericsson.se>
Date: 1997/09/24
Raw View
Hello all,

When converting our existing code base from cfront compatible code to
(almost) CD2 draft version C++, I've stumbled on a problem where a
template class is instantiated with a function with extern "C" linkage.

The situation is as follows:

template <class T>
class A
{
public:
  A(T* p, void (*func)(T*));
  ...
};

Now,  since linkage is part of the type, no extern "C" function is
allowed here. How can I instantiate my class A with a pointer to a
C function?

Is this not supposed to be legal? If not, why?
   _
/Bjorn.
--
Bjorn Fahller                  Tel: +46 8 4220898 /
NA/EBC/DN/NT                   -------------------
Ericsson Business Networks AB / A polar bear is a rectangular
S-131 89 Stockholm/SWEDEN    /  bear after a coordinate transform
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/09/25
Raw View
Bjorn Fahller <Bjorn.Fahller@ebc.ericsson.se> wrote in article
<3428B769.60A1@ebc.ericsson.se>...
> Hello all,
>
> When converting our existing code base from cfront compatible code to
> (almost) CD2 draft version C++, I've stumbled on a problem where a
> template class is instantiated with a function with extern "C" linkage.
>
> The situation is as follows:
>
> template <class T>
> class A
> {
> public:
>   A(T* p, void (*func)(T*));
>   ...
> };
>
> Now,  since linkage is part of the type, no extern "C" function is
> allowed here. How can I instantiate my class A with a pointer to a
> C function?
>
> Is this not supposed to be legal? If not, why?

Does this do it?

extern "C"
{
  template <class T> struct Helper
  {
    typedef void(*CFunc)(T*);
  };
}

template<class T> class A
{
   A(T* p, Helper<T>::CFunc func);
};
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Bjorn Fahller <Bjorn.Fahller@ebc.ericsson.se>
Date: 1997/09/26
Raw View
Bill Wade wrote:
>

> Does this do it?
>
> extern "C"
> {
>   template <class T> struct Helper
>   {
>     typedef void(*CFunc)(T*);
>   };
> }
>
> template<class T> class A
> {
>    A(T* p, Helper<T>::CFunc func);
> };

Unfortunately not, as I'm not allowed to declare an extern "C" template.
   _
/Bjorn.
--
Bjorn Fahller                  Tel: +46 8 4220898 /
NA/EBC/DN/NT                   -------------------
Ericsson Business Networks AB / A polar bear is a rectangular
S-131 89 Stockholm/SWEDEN    /  bear after a coordinate transform
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]