Языковая связь функции является частью ее типа:
7.5.1 [dcl.link] стандарта ISO C++:
Языковая связь по умолчанию для всех типов функций, имен функций и имен переменных — это языковая связь C++. Два типа функций с разными языковыми связями являются разными типами, даже если в остальном они идентичны.
Можно ли специализировать шаблон по типу привязки указателя на функцию или иным образом исследовать тип указателя на функцию, чтобы определить его привязку во время компиляции?
Эта первая попытка не кажется законной:
#include <iostream>
#include <typeinfo>
struct cpp {};
struct c {};
extern "C++" void foo()
{
std::cout << "foo" << std::endl;
}
extern "C" void bar()
{
std::cout << "bar" << std::endl;
}
template<typename> struct linkage;
template<>
struct linkage<void(*)()>
{
typedef cpp type;
};
template<>
struct linkage<extern "C" void(*)()>
{
typedef c type;
}
int main()
{
std::cout << "linkage of foo: " << typeid(linkage<decltype(&foo)>::type).name() << std::endl;
std::cout << "linkage of bar: " << typeid(linkage<decltype(&bar)>::type).name() << std::endl;
return 0;
}
g++-4.6
выходы:
$ g++ -std=c++0x test.cpp
test.cpp:26:38: error: template argument 1 is invalid
test.cpp:26:3: error: new types may not be defined in a return type
test.cpp:26:3: note: (perhaps a semicolon is missing after the definition of ‘<type error>’)
test.cpp:32:10: error: two or more data types in declaration of ‘main’
Есть ли какое-то приложение SFINAE, которое могло бы реализовать эту функциональность?