1

I have a class with template Inside this class there is a variable T * Variable; and variable string str; and there is function with part of the code:

if(std::is_same<T, string>::value){
    *Variable = str;
    return 0;
}

Naturally despite above asignment wont work on different variables due to condition before, compiler moans on compile time. I was thinking about writing conditional template function with two template arguments to put it in place of above asignment:

Assign(Variable, &str);

And before the class:

template <typename T, typename U>
conditional <T, U> void Assign(T* to, U* from){
    ;
}
conditional <string, string> void Assign(T* to, U* from){
    *to = *from;
}

So the compiler would choose specialized one for my needed string. How to formulate this? Can this be formulated just for variable types or do I need a third argument? Or maybe there can be some sort of template condition for just primary code inside function?

Gen0me
  • 115
  • 8
  • 4
    Can you use `C++17`? [`if constexpr`](https://en.cppreference.com/w/cpp/language/if) – tkausl Jan 24 '20 at 17:10
  • So far I should be compatible c++ 11, 14, 17 – Gen0me Jan 24 '20 at 17:16
  • Looks like an [xy problem](https://meta.stackexchange.com/a/66378) to me. Why do you need to assign a value conditionally based on type? Would it be possible to overload the function that this snippet comes from? – eike Jan 24 '20 at 17:23

3 Answers3

2

Just use template specialization:

template<typename T>
class Foo {
    T * Variable;

public:
    explicit Foo(T *p) : Variable{p}
    {}

    void Assign(const std::string& str) {
        std::cout << str << " No op\n";
    }
};

template<>
void Foo<std::string>::Assign(const std::string& str) {
    *Variable = str;
}

https://wandbox.org/permlink/Oi1bl8LCQQQ2LyJC

This will work with quite old C++ version (at least C++03).

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • So if I understand correctly: You have here heritage of function that you are overloading using template for just string? And you are list initializing varieable with p inside constructor? Hmm so this overloaded function should be overloaded outside of a Foo class. I need to use it inside this class. – Gen0me Jan 24 '20 at 18:43
  • @Gen0me This is not inheritance, but [template specialization](https://en.cppreference.com/w/cpp/language/template_specialization). The method is not overloaded, but the implementation is changed for the template parameter `std::string`. – eike Jan 24 '20 at 19:10
  • I solved the problem, but Im still curious: Will this method work for function calls inside a class – Gen0me Jan 24 '20 at 19:25
2

If you have access to C++17, then simply use if constexpr:

if constexpr (std::is_same<T, string>::value) {
    *Variable = str;
    return 0;
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
0

So I wrote the Assign function before my class with template and template specialization

template<typename T, typename U>
void Assign(T* to, U* from){
    ;
}
template<>
void Assign(string* to, string* from){
    *to = *from;
}

And I changed line *Variable = str; into:

Assign(Variable, &str);

Here is a topic that helped me: Templated Functions.. ERROR: template-id does not match any template declaration

Gen0me
  • 115
  • 8