在C++ 03中,模板参数推导在某些上下文中不会发生.例如:
template <typename T> struct B {};
template <typename T>
struct A
{
typedef B<T> type;
};
template <typename T>
void f(typename A<T>::type);
int main()
{
B<int> b;
f(b); // ERROR: no match
}
Run Code Online (Sandbox Code Playgroud)
这里int不推断T,因为嵌套类型A<T>::type是非推断的上下文.
我是否写过这样的函数:
template <typename T> struct B {};
template <typename T>
void f(B<T>);
int main()
{
B<int> b;
f(b);
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,因为B<T> 是推断的背景.
但是,在C++ 11中,模板别名可用于以类似于第二个示例的语法伪装嵌套类型.例如:
template <typename T> struct B {};
template <typename T>
struct A
{
typedef …Run Code Online (Sandbox Code Playgroud) 我有一个采用模板类型参数(tTRAIT)的类.我想知道tTRAIT 的模板类型 成员 别名,但我无法弄清楚语法.(这有可能吗?).
template <bool bBOOL>
struct SFoo {};
struct STrait
{
template <bool bBOOL>
using TFoo = SFoo<bBOOL>;
};
template <typename tTRAIT>
struct SBar
{
template <bool bBOOL>
friend typename tTRAIT::template TFoo<bBOOL>;
};
SBar<STrait> bar;
Run Code Online (Sandbox Code Playgroud)
Clang的错误(friend在线)是:
error: friend type templates must use an elaborated type
Run Code Online (Sandbox Code Playgroud)
我试过用尽我能想到的所有可能的组合:
friend tTRAIT::TFoo;
friend tTRAIT::template TFoo;
friend typename tTRAIT::TFoo;
friend typename tTRAIT::template TFoo;
template <bool bBOOL> friend tTRAIT::TFoo;
template <bool bBOOL> friend tTRAIT::TFoo<bBOOL>;
template <bool bBOOL> friend tTRAIT::template TFoo; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个包含许多函数对象的库,这些函数对象的类具有多个operator()重载,这些重载不依赖于类的状态而不会改变它.现在,我试图让我的代码使用许多旧式API(它不是随机需要,我实际上不得不处理这样的API),因此决定使函数对象可以转换为任何一个对应的函数指针重载.在某些时候,我意识到我有太多这样的转换来运行指针运算符,我理论上应该能够编写一个可变转换运算符.这是一个实现这种可变参数运算符的类:
struct foobar
{
template<typename... Args>
using fptr_t = void(*)(Args... args);
template<typename... Args>
operator fptr_t<Args...>() const
{
return [](Args... args) {
// Whatever
};
}
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用lambda转换函数指针来实现转换运算符,这不是问题,因为我拥有的每个函数对象都是无状态的.目标是能够使用如下类:
int main()
{
void(*foo)(int) = foobar();
void(*bar)(float, double) = foobar();
}
Run Code Online (Sandbox Code Playgroud)
g ++使用预期的语义编译此代码没有问题.但是,clang ++ 拒绝它时出现模板替换失败错误:
Run Code Online (Sandbox Code Playgroud)main.cpp:21:11: error: no viable conversion from 'foobar' to 'void (*)(int)' void(*foo)(int) = foobar(); ^ ~~~~~~~~ main.cpp:11:5: note: candidate function [with Args = int] operator fptr_t<Args...>() const ^ 1 error generated.
请注意,只要不涉及可变参数模板,clang ++对此类转换运算符没有任何问题.如果我使用单个模板参数,编译代码就没有问题.现在,编译器是接受还是拒绝上面的代码?
c++ function-pointers language-lawyer variadic-templates c++14