templates

Passing template function as argument for normal function

▼魔方 西西 提交于 2021-02-16 16:12:54
问题 I'm wondering if it's possible to pass a template function (or other) as an argument to a second function (which is not a template). Asking Google about this only seems to give info about the opposite ( Function passed as template argument ) The only relevant page I could find was http://www.beta.microsoft.com/VisualStudio/feedbackdetail/view/947754/compiler-error-on-passing-template-function-as-an-argument-to-a-function-with-ellipsis (not very helpful) I'm expecting something like: template

C++17 optional tree, error: invalid use of incomplete type

一笑奈何 提交于 2021-02-16 15:15:36
问题 When I compile a binary tree containing optional types: #include <optional> class BinaryTree { public: BinaryTree(); int value; std::optional<BinaryTree> left,right; }; int main() { return 0; } via g++ -std=c++17 -Wfatal-errors main.cpp I face with this error In file included from /usr/include/c++/7/bits/move.h:54:0, from /usr/include/c++/7/bits/stl_pair.h:59, from /usr/include/c++/7/utility:70, from /usr/include/c++/7/optional:36, from main.cpp:1: /usr/include/c++/7/type_traits: In

C++: How to solve template cyclic dependency between derived classes when two classes contain a pointer pointing to another?

£可爱£侵袭症+ 提交于 2021-02-16 13:58:07
问题 I have a parent class and some classes derived from it. I want to 'pair' two derived classes that eac has a pointer to another one. Code example: template<typename DerivedClassName> class Parent { // some stuff DerivedClassName* prtToPair; }; template<typename DerivedClassName> class DerivedA : public Parent<DerivedClassName> { }; template<typename DerivedClassName> class DerivedB : public Parent<DerivedClassName> { }; // compile fails DerivedA<DerivedB> dA; DerivedB<DerivedA> dB; dA

How to resolve ambiguity of call to overloaded function with literal 0 and pointer

℡╲_俬逩灬. 提交于 2021-02-16 10:00:28
问题 I'm pretty sure this must have been here already, but I didn't find much information on how to solve this kind of problem (without casting on the call): Given two overloads, I want that a call with function with a literal 0 always calls the unsigned int version: void func( unsigned int ) { cout << "unsigned int" << endl; } void func( void * ) { cout << "void *" << endl; } func( 0 ); // error: ambiguous call I understand why this happens, but I don't want to write func( 0u ) or even func(

How to resolve ambiguity of call to overloaded function with literal 0 and pointer

纵饮孤独 提交于 2021-02-16 09:59:56
问题 I'm pretty sure this must have been here already, but I didn't find much information on how to solve this kind of problem (without casting on the call): Given two overloads, I want that a call with function with a literal 0 always calls the unsigned int version: void func( unsigned int ) { cout << "unsigned int" << endl; } void func( void * ) { cout << "void *" << endl; } func( 0 ); // error: ambiguous call I understand why this happens, but I don't want to write func( 0u ) or even func(

Explicit template instantiation with variadic templates

£可爱£侵袭症+ 提交于 2021-02-16 04:48:39
问题 I have several template classes Impl (with some abstract methods) that are partially implemented in CPP files so that I need to explicitly instantiate my templates for the linker to find it, like this: template class Impl<T0>; template class Impl<T1>; template class Impl<T2>; ... template class Impl<Tx>; As the number of types Tx grows, I would like to find a better way than to manually expand these lists of explicit instantiations in all necessary files. I thought I could use variadic

Typedef in traits vs typedef in class

本小妞迷上赌 提交于 2021-02-15 11:45:39
问题 I'm looking through the Eigen source code for educational purposes. I've noticed that for each concrete class template X in the hierarchy, there is an internal::traits<X> defined. A typical example can be found in Matrix.h: namespace internal { template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > { typedef _Scalar Scalar; typedef Dense StorageKind; typedef DenseIndex Index;

Conditional compilation and non-type template parameters

旧街凉风 提交于 2021-02-15 06:18:08
问题 I am having trouble understanding non-type template arguments and was hoping someone could shed light on this. #include <iostream> template<typename T, int a> void f() { if (a == 1) { std::cout << "Hello\n"; } else { T("hello"); } } int main() { f<int, 1>; } When I compile this, I get an error saying: /tmp/conditional_templates.cc:13:12: required from here /tmp/conditional_templates.cc:8:5: error: cast from ‘const char*’ to ‘int’ loses precision [-fpermissive] T("hello"); ^ But, can't the

How to design a template class with switchable member variables at compile-time in C++?

老子叫甜甜 提交于 2021-02-11 18:21:32
问题 | ■ Probelm definition____________________ I'm trying to design a super-flexible, but memory efficient module satisfying below properties. It can switch OFF unnecessary member variables depending on the situation. Which variables it will own are determined at the compile-time . I somehow made one which determines its member list " before " the compile-time , using macros and enumerator flags . see below : ▼ TraitSwitch.h #pragma once // Macros to switch-off source codes themselves. #define ON

How to design a template class with switchable member variables at compile-time in C++?

此生再无相见时 提交于 2021-02-11 18:21:08
问题 | ■ Probelm definition____________________ I'm trying to design a super-flexible, but memory efficient module satisfying below properties. It can switch OFF unnecessary member variables depending on the situation. Which variables it will own are determined at the compile-time . I somehow made one which determines its member list " before " the compile-time , using macros and enumerator flags . see below : ▼ TraitSwitch.h #pragma once // Macros to switch-off source codes themselves. #define ON