template-specialization

Compiler doesn't see template specialization?

故事扮演 提交于 2019-12-11 11:04:43
问题 I have template in my .h file: template <typename T> void addToolsAreaItem(){ T* item = new T(_image, this); doSpecifiedStaff<T>(item); _tools.addTool(item); } and its specialization in .cpp file: template <> void ImageWorkspace::addToolsAreaItem<Preview>(){ _preview = std::make_unique<QGraphicsView>(_splitter); _imagesLayout.addWidget(_preview.get()); } Class Prewiew is empty and is used only for specialize behavior of one case (when preview button is toggled). But I get compiler error:

Out-of-line definition of constructor in fully-specialized template [duplicate]

断了今生、忘了曾经 提交于 2019-12-11 06:56:41
问题 This question already has an answer here : Explicit template specialization error (1 answer) Closed 2 years ago . I am really ashamed to ask this question, but it looks like I don't know anything about templates after all. So I have this snippet: template <typename> class foo; class bar; template <> class foo <bar> { public: foo(); }; template <> foo <bar> :: foo() { } Where, well, I just have a template class foo , a class bar , a specialization foo <bar> with a constructor, and I would like

Template specialization: C++ templates that only accept certain types (revisited)

删除回忆录丶 提交于 2019-12-11 06:14:41
问题 I want to make a simple message template class as follows. I followed the structure verbatim from the chosen answer posted here. However, this seems to break in Visual Studio 2013. template<typename T> Message; template<> Message <std::vector<uint8_t>> { public: }; template<> Message <std::string> { }; IntelliSense tells me that Message is not a template . Placing class before Message in the forward declaration results in a slightly better, but equally annoying IntelliSense error: expected

Avoiding duplicate symbol due to initialization of specialization in header?

风格不统一 提交于 2019-12-11 05:27:11
问题 I'm catching duplicate symbol errors due to definitions I am trying to provide in a header. Here's the error from the Minimal, Complete, and Verifiable example. The header files and source files are shown below. $ clang++ main.cpp x.cpp y.cpp -o main.exe 2>&1 | c++filt duplicate symbol Id<S>::id in: /tmp/main-3f2415.o /tmp/long long-d62c28.o duplicate symbol Id<T>::id in: /tmp/main-3f2415.o /tmp/long long-d62c28.o duplicate symbol Id<S>::id in: /tmp/main-3f2415.o /tmp/unsigned long long

c++ variadic template pack expansion by groups of arbitrary size

邮差的信 提交于 2019-12-11 04:54:41
问题 (Somehow related to this previous question) I want to evaluate the parameters of a template function by groups of N parameters. Something like this: template <size_t N, typename ... Ts> void evaluate(Ts const & ... fn) { for( int i=0; i<2; i++ ) runH<N>::func(i, fn...); } int main() { run<3>( // N = 2 [](int i){ cout << "lambda func 1: " << std::to_string( i ) << endl; }, [](int i){ cout << "lambda func 2: " << std::to_string( i ) << endl; }, [](int i){ cout << "lambda func 3: " << std::to

64-bit G++ 4.6.3 doesn't treat longs as long longs in specialised function templates, even though they're the same size. Is this a bug?

不羁的心 提交于 2019-12-11 04:54:37
问题 Consider the following code: #include <iostream> #include <cinttypes> template<class T> void f(); template<> inline void f<long long>() { std::cout<<"f<long long>()"<<std::endl; } int main(int , char** ) { std::cout<<"sizeof(long)="<<sizeof(long)<<std::endl; std::cout<<"sizeof(long long)="<<sizeof(long long)<<std::endl; f<int64_t>(); return 0; } 32-bit G++ 4.6.3 compiles this successfully and produces the output: sizeof(long)=4 sizeof(long long)=8 f<long long>() Compiling under 64-bit G++ 4.6

template-id does not match any template delcaration

旧时模样 提交于 2019-12-11 04:15:16
问题 I'm getting a frustrating compiler error I can't seem to work around. It's to do with the template specialization but I can't see what's wrong... ../../include/thread/lock_guard.inl:23: error: template-id 'lock_guard<>' for 'thread::lock_guard<thread::null_mutex>::lock_guard(thread::null_mutex&)' does not match any template declaration ../../include/thread/lock_guard.inl:23: error: invalid function declaration ../../include/thread/lock_guard.inl:29: error: template-id 'lock_guard<>' for

Explicit Template Function and Method Specialization

冷暖自知 提交于 2019-12-10 23:18:29
问题 I've been looking for a clear answer, and I'm just catching bits and pieces from the web. I've got a function, and it needs to act differently based on a type variable. The function takes no arguments, so overloading doesn't work, leading to template specialization. E.g.: //Calls to this function would work like this: int a = f(); int b = f<int>(); int c = f<char>(); //... First off, is that even syntactically possible? I feel like it is. Continuing. I'm having problems defining this function

C++ template metaprogramming static type checking

ぐ巨炮叔叔 提交于 2019-12-10 21:52:22
问题 I couldn't find an answer to my problem so I post it as a question. I make a small dummy example to explain it: enum STORAGE_TYPE { CONTIGUOUS, NON_CONTIGUOUS }; template <typename T, STORAGE_TYPE type=CONTIGUOUS> class Data { public: void a() { return 1; } }; // partial type specialization template <typename T> class Data<T, NON_CONTIGUOUS> { public: void b() { return 0; } }; // this method should accept any Data including specializations… template <typename T, STORAGE_TYPE type> void func

Is a templated and a nontemplated version of the same function considered an overload?

强颜欢笑 提交于 2019-12-10 20:58:33
问题 A very formal question: is this considered an overload? Is removing the template fundamentally different than only overloading on arguments? template<class T> void myFunction(const T& t) {} void myFunction(const double& t) {} Then the follow up question, is it better to follow this approach or to use template specialization, instead of the overload? template<> void myFunction(const double& t) {} 回答1: First of all, according to the standard (start of §13): “When two or more different