0
#include<vector>
#include<functional>

class SegmentTree final{
    public:
        template<typename T> using data_type=T;
    public:
        template<typename T=int>
        explicit SegmentTree(const size_type& size);
};

I want that SegmentTree isn't a template class but has template type, and deduce this type from constructor. Can I do it?

  • 1
    What will `SegmentTree` do with the template type? Since `SegmentTree` isn't a class template you can't have any data members making use of a template type. – Kevin Sep 12 '21 at 01:13
  • Nothing. But, I think if it is accepted, I store this classes in array with many other data types. Can't I do it? it's so sad... –  Sep 12 '21 at 01:21
  • 1
    Can you give a clear example of what you want? Something that might fit is having a base class that isn't a class template, and have a class template derived class. Your array would store pointers (preferrably smart pointers) of the base class, and the real objects would be the templated derived types. But I don't know if that's what you're looking for. – Kevin Sep 12 '21 at 01:29
  • 1
    "I store this class in array with many other data types". Arrays are homogeneous. You can use a `std::tuple` if you mean to store some constant sequence of data. You can use `std::variant` to store one of a few possibilities. You can use `std::any` to store literally any value. – Silvio Mayolo Sep 12 '21 at 01:33
  • Thank you for answer. I just want to know it is possible. I'm studying! –  Sep 12 '21 at 01:42
  • 1
    Does this answer your question? [Why not infer template parameter from constructor?](https://stackoverflow.com/questions/984394/why-not-infer-template-parameter-from-constructor) You can do deduce template parameters in the constructor in c++17. – francesco Sep 12 '21 at 01:53
  • What I want to know is I do deduce inner template parameter in class with constructor. I think this link doesn't match with my question.. But Thank you for kind answer!! –  Sep 12 '21 at 05:40
  • 1
    To me this seems like a classical XY. You should always define the problem first and only then how to solve it. In your case, if a type is deduced from a constructor, how does your class differ in function from a regular templated class? Should it only look syntactically different? – Aziuth Sep 12 '21 at 08:12

1 Answers1

1

There is no such thing as a inner template parameters for data members. If a data member depends on a type template parameter, such a parameter must be available in the signature of the class. That does not mean, however, that you need to specify all template parameters when instantiating a class, because, in c++17, they can be deduced by the constructor, see here.

Example:

#include <string>

template <typename U>
class A {
    U m_data;
public:
    A(U data) : m_data{data} { }
};

int main()
{
    A a(3); // Here U = int
    A b(std::string("hello")); // Here U = std::string

    return 0;
}

Test it live on Coliru.

In the code above, I do not need (although I could) specify the template parameter U when creating the variables a and b. This is because the type of the variable passed to the constructor is used to deduce U for the entire class. Still, A is a template class.

francesco
  • 7,189
  • 7
  • 22
  • 49