- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的代码中有以下方案,
#include <memory>
#include <iostream>
template <class T>
struct A {};
template <class T>
struct Z {};
template <class T, class U>
struct B : A<T>, Z<U> {};
template <class T>
struct C : B<T, T> {};
template <class T>
void foo(const std::shared_ptr<A<T>>& a)
{
std::cout << "HI !" << std::endl;
}
int main()
{
auto c = std::make_shared<C<char>>();
foo(c);
return 0;
}
但是编译器无法正确替换模板参数:
main.cpp: In function 'int main()':
main.cpp:26:10: error: no matching function for call to 'foo(std::shared_ptr<C<char> >&)'
foo(c);
^
main.cpp:17:6: note: candidate: template<class T> void foo(const std::shared_ptr<A<T> >&)
void foo(const std::shared_ptr<A<T>>& a)
^~~
main.cpp:17:6: note: template argument deduction/substitution failed:
main.cpp:26:10: note: mismatched types 'A<T>' and 'C<char>'
foo(c);
^
所以我将 foo
更改为:
template <class T, template <class> class U>
void foo(const std::shared_ptr<U<T>>& a)
{
std::cout << "HI !" << std::endl;
}
它可以工作,但感觉不对,因为现在对类型的模板约束非常松散,通常最终会产生更困惑的错误消息。
是否有更好的方法来处理此类模板推导失败?
最佳答案
有办法。您可以选择使用一些模板元编程手动约束模板,以便仅指针类型(可以扩展以使用各种指针类型,但仅适用于 unique_ptr
和 shared_ptr
在这种情况下(实际上所有具有 element_type
类型别名的智能指针类型)都可以使用派生类。
#include <memory>
#include <iostream>
namespace {
/**
* Get the template type of a template template type
*/
template <typename T>
struct GetType;
template <typename T, template <typename...> class TT>
struct GetType<TT<T>> {
using type = T;
};
} // namespace <anonymous>
template <class T>
struct A {};
template <class T>
struct Z {};
template <class T, class U>
struct B : A<T>, Z<U> {};
template <class T>
struct C : B<T, T> {};
template <class T, typename std::enable_if_t<std::is_base_of<
A<typename GetType<typename std::decay_t<T>::element_type>::type>,
typename std::decay_t<T>::element_type>::value>* = nullptr>
void foo(const T&)
{
std::cout << "HI !" << std::endl;
}
int main()
{
auto c = std::make_shared<C<char>>();
auto c_uptr = std::make_unique<C<char>>();
foo(c);
foo(c_uptr);
return 0;
}
这现在可以与 unique_ptr
和 shared_ptr
一起使用。您可以通过使用 decltype
和 .get()
检查包含的指针类型(示例中的 element_type
)来选择使其更通用在智能指针上,并创建一个特征来检测传递的指针是否是原始指针,如果是则只使用类型本身。但你明白了。
关于c++ - 模板的模板推导失败(中间有继承),有没有更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44615336/
据我所知,根本不为元素呈现 HTML,或添加 display:none,似乎具有完全相同的行为:两者都使元素消失并且不与 HTML 交互。 我正在尝试禁用和隐藏一个复选框。所以HTML的总量很小;我无
我刚刚读了Android Architecture Tutorial: Developing an App with a Background Service (using IPC) .基本上是 让服
我有两个查询具有相同的结果,现在我想知道哪个查询更优化? 在选择中: select t1.*, sum(t2.value) as total_votes from table1 t1 left joi
有人告诉我,对于 I/O 绑定(bind)的应用程序,非阻塞 I/O 会更好。对于 CPU 密集型应用程序,阻塞 I/O 会好得多。我找不到这种说法的原因。试过谷歌,但很少有文章只是触及这个话题而没有
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我从 API 收到一个 json,我需要解析并修改一个属性值。问题是,我收到的 json 数据的嵌套结构不一致,我无法控制它。 这将禁止我指定在特定深度(如 parsedJson.children[0
我有 451 个城市的坐标。现在我想计算每个城市之间的距离,然后根据该距离对一些结果进行排序。现在我有两个选择: 我可以运行一个循环来计算每个可能的城市组合的距离并将它们存储到一个表中,这将产生大约
对于返回相同结果的不同查询,我有两个查询计划我想知道是否有人可以告诉我哪个“更好”,以及为什么。 SELECT * FROM bids order by (select ranking from us
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我有一个二维数组。我需要尽可能快地对其执行一些操作(函数每秒将被调用十几次,所以让它变得高效会很好)。 现在,假设我想获取元素 A[i][j],简单地使用 A[i][j] 在速度上有什么不同吗和 *(
在声明或使用字符串的代码中,我通常会看到开发人员这样声明它: string randomString = @"C:\Random\RandomFolder\ThisFile.xml"; 代替: str
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why don't CSS resets use '*' to cover all elements? 我正
如果我有一个包含许多重复项的 python 列表,并且我想遍历每个项目,而不是重复项,最好使用一个集合(如 set(mylist),或者找到另一种方法来创建没有重复的列表?我想只是循环遍历列表并检查重
在阅读常量接口(interface)反模式时,我发现没有实例的最终常量类比常量接口(interface)更好。 请解释一下怎么做? public interface ConstIfc { publ
我正在查看我继承的一些旧代码,我真的不喜欢某些地方的风格。我真的不喜欢它的外观的一件事是: bool func() { bool ret = true; ret &= test1();
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我经常发现自己试图使用 boost/QT 信号解耦对象。实现这一点的简单方法是针对我要通信的每个具体类型,创建一个新的信号和插槽签名并连接所有相关对象。这导致了访问者模式,理想情况下我想发出一个访问者
我正在 https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html 上阅读有关 lambda 的内容 在方法
public List getInts() { List xs = new ArrayList(); xs.add(1); // return Collections.unmo
我是一名优秀的程序员,十分优秀!