为什么我的函数不尝试解析到不兼容的模板函数,而默认解析到常规函数?
std::string nonSpecStr = "non specialized func"; std::string const nonTemplateStr = "non template func"; class Base {}; class Derived : public Base {}; template std::string func(T * i_obj) { ( * i_obj) += 1; return nonSpecStr; } std::string func(Base * i_obj) { return nonTemplateStr; } void run() { // Function resolution order // 1. non-template functions // 2. specialized template functions // 3. template fun
0 2023-05-15
编程技术问答社区
将一个非stexpr积分值调整为一个非类型的模板参数,以及代码膨胀
考虑一个函数对象F服用constexpr size_t参数I struct F { template constexpr size_t operator()(size ) const { return I; } }; 包裹在类型size 中,其中(简洁) template using size = std::integral_constant ; 当然,我们可以直接传递I,但我想强调,它是通过将其用作模板参数来强调的.函数F在这里是虚拟的,但实际上它可以执行各种有用的事情,例如从元素的I元素中检索信息. F无论I> I,都假定具有相同的返回类型. I可能是任何积分类型,但假定为非负类型. 问题 给定constexpr size_t值I,我们可以通过 调用F F()(size ()); 现在,如果我们想用非constepr size_t value i
10 2023-03-28
编程技术问答社区
具有专门功能的拉开式功能类型
template struct function_args {}; template struct function_args { using type = tuple; }; template using decltypeargs = typename function_args::type; 当我研究这里所做的事情时,我试图重写function_args.我试图使用功能来执行此操作,以消除对decltypeargs模板的需求.但发现自己陷入不正确的语法中: template tuple myTry(); template tuple myTry(); 我希望打电话de
6 2023-03-26
编程技术问答社区
为什么包括在没有模板声明的模板类中使用模板参数的友方函数的实现会被编译?
给定以下代码. #include template class Foo { public: Foo(const T& value = T()); friend Foo operator+ (const Foo& lhs, const Foo& rhs) { // ... } friend std::ostream& operator& x) { // ... } private: T value_; }; 编译器毫无困难地编译了具有模板参数的两个朋友函数,而没有以下语法 template friend Foo operator+ (const Foo& lhs, const Foo& rhs) 或 frien
14 2023-03-22
编程技术问答社区
模板类特定类型函数
好,所以我有这个模板类,有点像单向列表. template List 它具有此内部功能打印 public: void Print(); 您可以猜到,从开始到结尾都打印列表内容; 但是,由于模板可以上课,因此可以想象,在这种情况下,我需要不同的print()实现.例如,我还有另一个班级 class Point{ private: int x, y; public: int getX(); int getY(); } 所以我想专门为点设计打印.我尝试了: void List::Print(); 但编译器告诉我 prototype for void List Print() doesn match any in class List 虽然 candidates are: from List [with T = Point] void List
6 2023-03-11
编程技术问答社区
使用decltype的函数参数类型
注意:此问题中提供的示例不是生产代码,根本没有意义.只是在那里说明我的问题. i正在测试decltype的可能性,尤其是当它用于推断功能参数类型时,并遇到了问题: 假设有两个类似的类: struct ClassInt { // Note: no default ctor ClassInt(int value) : m_Value(value) {} int m_Value; }; struct ClassDouble { // Note: no default ctor ClassDouble(double value) : m_Value(value) {} double m_Value; }; 现在,我编写了一个(某种程度上)通过字符串检索类型参数(应为上述一个)的实例,并将给定值分配给其m_Value成员: template
48 2023-03-02
编程技术问答社区
为什么C++11不把lambdas隐式转换为std::函数对象?
我实现了一个通用事件发射机类,该类允许代码注册回调,并用参数发射事件.我使用boost.任何类型擦除来存储回调,以便它们可以具有任意参数签名. 这一切都起作用,但是由于某种原因,必须首先将lambdas转入std::function对象.编译器为什么不推断lambda是函数类型?是因为我使用variadic模板的方式吗? 我使用clang(版本字符串:Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)). 代码: #include #include #include #include #include #include using std::cout; using std::endl; using std::function; using std::map;
4 2023-03-02
编程技术问答社区
模板函数对其他函数的调用
我了解template functions通常在标题文件中声明和定义. 我遇到的问题是我的template function呼叫其他功能.这些其他功能的原型在 本身之前都在同一标头文件中. 代码的那部分: //header.h template __global__ void MCMLKernel(SimState d_state, GPUThreadStates tstates) { // photon structure stored in registers PhotonStructGPU photon; // random number seeds UINT64 rnd_x; UINT32 rnd_a; // Flag to indicate if this thread is active UINT32 is_active; // Restore the thread sta
10 2023-03-02
编程技术问答社区
模板中的类型使用不合法
我是模板的新手.我不知道我在做什么错: #include "stdafx.h" #include using namespace std; template void inc(T* data) { (*T)++; } int main() { char x = 'x'; int b = 1602; inc(&x); inc(&b); cout > a; return 0; } 在VS2013中编译后,我有一个错误: 错误1错误C2275:'t':这种类型的非法使用为表达式 解决方案 也许您应该: template void inc(T* data) { (*data)++; } 其他解决方案
8 2023-03-02
编程技术问答社区
从C文件中调用一个模板函数
我知道模板未在C中定义.但是,就我的情况而言,我的API用C ++编写,该API由C中编写的应用程序使用.我希望在API中添加模板函数.该函数定义为ABC.CPP中的如下: template T function_name(T param1){ ... ... return val; } abc.hpp中的声明如下: template T function_name(T); ,此功能从xyz.c称为: int a ,b = 5; a = function_name(b); 但是,它在ABC.CPP和ABC.HPP中显示以下错误: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ 即使使用Extern" C"也无济于事(导致错误:带有C链接的模板).我
16 2023-03-01
编程技术问答社区
为什么浮点类型对模板函数来说是无效的模板参数类型?
我在网站上阅读了声明 template int func() { return x; } 是有效的,而以下内容不是 template double func() { return x; } 为什么第一个是模板函数的法律声明,而第二个则不是? 解决方案 它是无效的,因为它不是积分类型.对非类型模板参数有一定的限制,这是其中之一,说... 浮点数和类式对象不允许作为非型模板参数. template // ERROR: floating-point values are not double process(double v) { // allowed as template parameters return v * VAT; } template // ERROR: class-type objects are not
10 2023-03-01
编程技术问答社区
隐藏库中的模板函数声明
首先,大局.我有一个记录器课.我为课程创建了一个简化的接口,并为接口创建了一个库.我想使用PIMPL隐藏Logger类实现,因此用户不需要记录器的标题.我在模板功能方面度过了糟糕的时光... 记录器标头的定义如下 /* Logger.h */ class Logger { public: virtual ~Logger(){}; public: template void log(const char* fmt, const Args&... args) { printf(fmt, &args...); } }; std::shared_ptr create_logger() { return std::shared_ptr(new Logger()); } 第一版 我已经创建了这样的接口 /* LoggerInterface.h
8 2023-03-01
编程技术问答社区
为什么我的函数不尝试解析到不兼容的模板函数,而默认解析到常规函数?
std::string nonSpecStr = "non specialized func"; std::string const nonTemplateStr = "non template func"; class Base {}; class Derived : public Base {}; template std::string func(T * i_obj) { ( * i_obj) += 1; return nonSpecStr; } std::string func(Base * i_obj) { return nonTemplateStr; } void run() { // Function resolution order // 1. non-template functions // 2. specialized template functions // 3. template fun
4 2023-02-28
编程技术问答社区
为什么模板函数不能把指向派生类的指针解析为指向基类的指针?
编译器在编译时是否无法将指针用于派生的类,并且知道它具有基类?,它似乎是基于以下测试的.关于问题发生的地方,请参阅我的评论. 我该如何工作? std::string nonSpecStr = "non specialized func"; std::string const specStr = "specialized func"; std::string const nonTemplateStr = "non template func"; class Base {}; class Derived : public Base {}; class OtherClass {}; template std::string func(T * i_obj) { return nonSpecStr; } template std::string func(Base * i_obj) { return specStr; } std::
14 2023-02-24
编程技术问答社区
如何编写一个模板函数,接收一个数组和一个指定数组大小的int
对于大学练习,我被要求编写一个模板函数" print();",该函数需要两个参数,1:一个通用类型的数组和2:指定数组大小的int.然后,该函数应打印出数组中的每个项目.我在函数参数方面遇到了一些麻烦.我目前拥有的代码是: template Type print (Type a, Type b) { Type items; Type array; a = array; b = items; for (int i = 0; i
22 2023-02-24
编程技术问答社区
模板类的朋友函数
我有以下模板类和模板功能,该功能打算访问类的私人数据成员: #include template class MyVar { int x; }; template void printVar(const MyVar& var) { std::cout void scanVar(MyVar& var) { std::cin >> var.x; } struct Foo {}; int main(void) { MyVar a; scanVar(a); printVar(a); return 0; } 要将这两个函数声明为MyVar的朋友函数,我尝试了template class MyVar声明内的以下方式来声明友谊.他们都没有工作
10 2023-02-24
编程技术问答社区