草庐IT

Templates

全部标签

c++ - 从使用模板的类继承时如何覆盖方法?

我在尝试弄清楚如何正确编写既继承自使用模板的类又重写虚拟方法的类时遇到了麻烦。当我尝试创建此类的实例时,VisualStudio给我一个错误,指出objectofabstracttypePropertyRealisnotallowed:purevirtualfunction"Property::propertyId[widtht=qreal]"hasnooverrider这是我的代码templateclassProperty{T_value;public:Property(TinitValue);~Property();virtualQStringpropertyId()=0;virt

c++ - 通用 lambda、重载、std::is_invocable 和 SFINAE - GCC 和 Clang 之间的不同行为

问题我写了一段可以编译的复杂模板代码withGCC8.2.1,但不是withClang7.0(代码和错误链接)。我认为这可能是thisQ&A的暗示,但我看不到它。动机我正在编写一个类,我希望它可以用两个不同类型的可调用对象构造,但也可以省略其中一个,即:my_class(callable_1);my_class(callable_2);my_class(callable_1,callable_2);那应该没有问题。但是,为什么不允许callable_1和callable_2成为函数模板(或带有operator()模板的仿函数)。也就是说,我想要这个(或者至少最初想要):my_class

c++ - 将函数传递给可变函数模板

考虑以下函数模板:templatevoidfoo0(std::functionf){}templatevoidfoo1(std::functionf){}以及以下函数:voidbar(intn){}为什么会出现以下情况:foo0(bar);//doesnotcompilefoo1(bar);//compilesfine编译错误是(gcc-8withC++17):error:nomatchingfunctionforcallto'foo0(void(&)(int))'foo0(bar);^note:candidate:'templatevoidfoo0(std::function)'vo

c++14 static constexpr auto 与 odr 用法

我有以下C++14代码:templatestructTest{staticconstexprautosomething{T::foo()};};这很好,只要T::foo()也是一个constexpr。现在我知道something是ODR使用的,所以我需要提供命名空间声明。我应该使用什么语法?templateconstexprautoTest::something;不起作用。谢谢! 最佳答案 通过using定义的类型名怎么样?templatestructTest{usingsomeType=decltype(T::foo());sta

c++ - 初始值设定项列表的模板替换错误

我知道如何使下面的代码工作:我只是取消注释Printer的第二个构造函数。想法很简单:我想编写一个构造函数/函数,它可以采用存储在一些我可以迭代的抽象数据结构中的多个参数。我希望它至少适用于vector和列表(确实如此),但也适用于初始化列表(但它不适用)。我使用以下简单语法(可能比我想要的更通用,我不使用模板模板)所以我不必编写可变参数模板来处理std::的分配器类型:#include#include#include#includeusingnamespacestd;structPrinter{templatePrinter(constContainer&cont){for(cons

c++ - 缩小 C++ 概念以排除某些类型

假设我想重载ostream的左移运算符s和所有容器。这是我目前拥有的(用-fconcepts编译):#include#includetemplateconceptboolIterable=requires(Containert){{*t.begin()++,t.end()};};templatestd::ostream&operatora={1,2,3};std::cout然而,问题在于,这已经是一个版本的ostream&对于std::string.是否有通用的(类似于requiresnot表达式)或特定的(可能类似于我可以排除具体类的偏特化)方法来排除概念中的某些内容?如果不是,正确

c++ - 模板化友元声明在 g++ 5.4.0 下不起作用——编译器错误或错误代码?

下面是一些C++代码,在我的Mac(Xcode10.210E125/AppleLLVM版本10.0.1(clang-1001.0.46.4))上编译没有错误,但给出了编译器错误消息(显示下面)在我的Linux机器上(g++(Ubuntu5.4.0-6ubuntu1~16.04.11)5.4.020160609)。我的问题是,这是g++5.4.0中的编译器错误,还是我在代码中做错了什么?g++5.4.0的编译器错误是:$g++template_friend.cpptemplate_friend.cpp:Ininstantiationof‘classSubClass’:template_f

c++ - 推导用户定义值模板参数(C++2a,P0732R2)

我正在尝试使用带有-std=c++2a的GCC9.1来获取推导的用户定义类的模板参数的值(http://wg21.link/p0732r2)。structuser_type{inta;constexpruser_type(inta):a(a){}};templatestructvalue{};templatevoidf(valuearg){}voidg(){f(value());//errorhere}编译器资源管理器:https://godbolt.org/z/6v_p_R我得到错误:source>:8:30:note:templateargumentdeduction/substi

c++ - 模板类中引用的引用类型是什么

这个问题在这里已经有了答案:Conciseexplanationofreferencecollapsingrulesrequested:(1)A&&->A&,(2)A&&&->A&,(3)A&&&->A&,and(4)A&&&&->A&&(2个答案)关闭3年前。在下面的代码中,a和b的类型是什么?templatestructA{T&a;Tb;};intmain(){inti=1;Aa{i,i};return1;}我使用了这篇文章中的代码,它可以给出变量的类型。->post但是,它说这两种类型都是iconst&。intmain(){inti=1;Aa{i,i};std::cout()()

带有 std::enable_if 的 C++ 可变参数模板部分模板特化

ITNOA我的问题是如何在可变参数模板部分模板特化场景中使用std::enable_if?例如,我有一个类使用如下所示的可变参数模板部分特化/***Commoncase.*/templatestructfoo;/***Finalsuperclassforfoo.*/templatestructfoo{voidfunc(){}};/***Regularfooclass.*/templatestructfoo:publicfoo{typedefsuperfoo;voidfunc(){coutsuper::templatefunc();}}它工作正常,但如果H是整数类型,我想要特定的部分特化