template-function

Template function calls to other functions

眉间皱痕 提交于 2020-01-06 14:48:06
问题 I understand the template functions usually are to be declared and defined in header files. The problem I am having is that my template function makes calls to other functions. The prototypes of those other functions are in the same header file before the template function itself. That portion of the code: //header.h template <int ignoreAdetection> __global__ void MCMLKernel(SimState d_state, GPUThreadStates tstates) { // photon structure stored in registers PhotonStructGPU photon; // random

Illegal use of type in template [closed]

帅比萌擦擦* 提交于 2019-12-12 01:36:41
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm new to templates. I can't figure out what am i doing wrong: #include "stdafx.h" #include <iostream> using namespace std; template <typename T> void inc(T* data) { (*T)++; } int main() { char x = 'x'; int b = 1602; inc<char>(&x); inc<int>(&b); cout << x << ", " << b << endl; int a = 0; cin >> a; return 0; }

Function parameter type using decltype

浪尽此生 提交于 2019-12-10 14:39:38
问题 Note: the example provided in this question is not production code and has no sense at all. It is just there to illustrate my problem. I was testing the possibilities of decltype , especially if it is used to deduce function parameter types, and ran into a problem: Suppose there were two classes structured like this: 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

Comparison operator for std::vector<T> fails to find comparison operator for T

廉价感情. 提交于 2019-12-09 09:09:09
问题 The following very simple code won't compile #include <vector> #include <string> namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ return lhs.f == rhs.f && lhs.uuid == rhs.uuid; } int main(){ std::vector<Foobar::Test> a; std::vector<Foobar::Test> b; if(a==b){ } return 0; } https://godbolt.org/g/zn6UgJ Won't compile in any of the compilers I have. While the following #include <vector> #include <string>

using and overloading a template member function of a base class?

邮差的信 提交于 2019-12-08 22:10:17
问题 In the following, struct Y overloads X 's member function f . Both overloads are template functions, but take different arguments ( typename and int ), to be explicitly specified: struct X { template <typename> static bool f() { return true; } }; struct Y : public X { using X::f; template <int> static bool f() { return false; } }; int main() { std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl; } This prints 1 0 using gcc, as expected. However, clang (3.3) complains that [...] error

Why isn't automatic downcasting applied to template functions?

北战南征 提交于 2019-12-08 18:48:48
问题 Someone asked this question about string appending. It's string s; s = s + 2; not compiling. People gave answers stating that operator+ is defined as a template function while operator+= is not, so auto downcasting ( int(2) to char(2) ) is not applied. The prototypes are template<typename _CharT, typename _Traits, typename _Alloc> class basic_string{ basic_string& operator+=(_CharT __c); }; template<typename _CharT, typename _Traits, typename _Alloc> inline basic_string<_CharT, _Traits,

C++: candidate template ignored: invalid explicitly-specified argument for template parameter

Deadly 提交于 2019-12-08 16:35:10
问题 I have this function header: template < bool src_alpha, int sbpp, int dbpp, typename T1, typename T2, Color (*getFunc)(T1 data, Uint8* addr), void (*putFunc)(T2 data, Uint8* addr, Color c) > static void OperateOnSurfaces(T1 data1, T2 data2, SDL_Surface * bmpDest, SDL_Surface * bmpSrc, SDL_Rect& rDest, SDL_Rect& rSrc) This is how I use it: OperateOnSurfaces< true, 32, 32, SDL_PixelFormat*, SDL_PixelFormat*, GetPixel<true,32>, PutPixel<true,true,32> >( bmpSrc->format, bmpDest->format, bmpDest,

hide template function declaration in library

早过忘川 提交于 2019-12-08 12:17:50
问题 First, the big picture. I have a Logger class. I've created a simplified interface for the class, and created a library for the interface. I'd like to use pimpl to hide the Logger class implementation, so the user doesn't need the Logger's headers. I'm having a bad time with template functions... The Logger header is defined like this /* Logger.h */ class Logger { public: virtual ~Logger(){}; public: template <typename... Args> void log(const char* fmt, const Args&... args) { printf(fmt,

Template class type-specific functions

假如想象 提交于 2019-12-07 16:44:27
问题 Ok, so i have this template class, which is kinda like one-way list. template <typename T> List and it have this inside function print public: void Print(); which, as you can guess, prints the list contents from begining to end; However, as template can take classes as T, one can imagine, that i would need different implementations of Print() for that very cases. For example, I have another class Point class Point{ private: int x, y; public: int getX(); int getY(); } so i want Print

Why doesn't my function skip trying to resolve to the incompatible template function, and default to resolving to the regular function? [duplicate]

泄露秘密 提交于 2019-12-07 10:40:38
问题 This question already has an answer here : Why can't a template function resolve a pointer to a derived class to be a pointer to a base class (1 answer) Closed 5 years ago . std::string nonSpecStr = "non specialized func"; std::string const nonTemplateStr = "non template func"; class Base {}; class Derived : public Base {}; template <typename T> std::string func(T * i_obj) { ( * i_obj) += 1; return nonSpecStr; } std::string func(Base * i_obj) { return nonTemplateStr; } void run() { //