type traits / linkage and generic programming

[Apologies for duplicates, I don't know if an earlier posting went through because I wasn't subscribed under this email address] Dear boosters: According to the C++ standard, language linkage is part of the specification of a type and even though on many platforms two function types with different linkage specs (eg. C and C++ language linkage) are the same, the standard states rather unambiguously ([7.5 decl.link]) "Two function types with different language linkages are distinct types even if they are otherwise identical." My question: does anyone know of a technique to determine the linkage of a function type at compile time? I'm not sure that that would matter for my purposes, but the question arose and I couldn't find an answer. Thanks for any feedback, --Herve Bronnimann PS: here's a concrete example of what I'm asking, a metafunction LinkageTraits that would make the following example compile (code obviously untested - all errors mine): #include <boost/static_assert.h> #include <boost/type_traits.h> struct CPPLinkageTag {}; struct CLinkageTag {}; extern "C" { typedef void functionWithCLinkageType(); } typedef void functionWithCPPLinkageType(); main() { BOOST_STATIC_ASSERT( boost::is_same<LinkageTraits<functionWithCLinkageType>::type, CLinkageTag >::value ); BOOST_STATIC_ASSERT( boost::is_same<LinkageTraits<functionWithCPPLinkageType>::type, CPPLinkageTag >::value ); }

Herve Bronnimann wrote:
[Apologies for duplicates, I don't know if an earlier posting went through because I wasn't subscribed under this email address]
Dear boosters: According to the C++ standard, language linkage is part of the specification of a type and even though on many platforms two function types with different linkage specs (eg. C and C++ language linkage) are the same, the standard states rather unambiguously ([7.5 decl.link]) "Two function types with different language linkages are distinct types even if they are otherwise identical."
My question: does anyone know of a technique to determine the linkage of a function type at compile time? I'm not sure that that would matter for my purposes, but the question arose and I couldn't find an answer. Thanks for any feedback, --Herve Bronnimann
I think it's not easy. In the past I've tried creating partial-specialisations on the linkage type, but it didn't work :-( This could be fixed with more recent compilers though. Overload resolution does work up to a point: typdef extern "C" int (cp*)(int); true_type is_c_linkage(cp); false_type is_c_linkage(int (*)(int)); But the problem is you can't declare something like: template<class R, class T> true_type is_c_linkage(extern "C" R(*)(T)); so you can't write a more generic solution :-( You could try: template <class F> struct is_c_linkage : public mpl::false_{}; extern "C" { template <class R, class T> struct is_c_linkage<R(*)(T)> : public mpl::true_{}; } and see if it works or not (obviously only on a compiler that differentiates between C and C++ linkage). HTH, John.
participants (3)
-
Herve Bronnimann
-
John Maddock
-
Peter Dimov