I want to create a class that is implicitly converted to another class with a template parameter. Here is the MCE that I want to achieve:
#include <iostream> template <typename T> class A { T value; public: A(T value) {this->value = value;} T getValue() const {return value;} }; class B { int value; public: B(int value) {this->value = value;} operator A<int>() const {return A(value);} }; template <typename T> void F(A<T> a) {std::cout << a.getValue() << std::endl;} void G(A<int> a) {std::cout << a.getValue() << std::endl;} int main() { B b(42); F(b); // Incorrect F((A<int>)b); // Correct G(b); // Also correct }
Is it possible to use the example F(b) if both class A and void F are library functions and cannot be changed?
F(b)
class A
void F
It:
F(b);
subtract the template argument. As a result, he will not do what you want.
Try explicitly specifying an argument template, for example:
F<int>(b);
since you provide the operator () in class B
()
class B
Source: https://habr.com/ru/post/1273606/More articles:Problems with static binding and dynamic binding in Java - javaCan an Ada range be declared an unlimited upper bound? - adaSort old Contnrs.TObjectList on Win64 - 64bitFind adjacent lines matching condition - rJava 9 HttpClient java.lang.NoClassDefFoundError: jdk / incubator / http / HttpClient - javaEditing a list inside a template is not saved - c ++MKMapView ignores safe zone on iOS 11 and iPhone X - iosImplementing custom primitive types in Julia - julia-langHow to add futon (not fauxton) to CouchDB - couchdbLearning NodeJS and MongoDB Docker - node.jsAll Articles