2

if I have a class that's templated, and has a single templated constructor:

template <typename Tdst>
struct memsrc {
    template <typename Tsrc>
    memsrc(const Tsrc *src, ssize_t len);

};

And I have an instance of the class in another class that's also similarly templated:

template <typename Tdst>
struct other {
   template <typename Tsrc>
   other();

   memsrc<Tdst> src_;
};

And I want to initialize src_ in the constructor of other, how do I do that? This:

src_ = memsrc<Tdst>::memsrc<Tsrc>(nullptr, 0); 

Doesn't work:

rawio.h: In constructor ‘filesrc<Tdst>::filesrc(rawfile*)’:
rawio.h:578:49: error: expected primary-expression before ‘>’ token
             mmapsrc_ = memsrc<Tdst>::memsrc<Tsrc>(mmap_.ptr(), mmap_.size());
gct
  • 14,100
  • 15
  • 68
  • 107

1 Answers1

0

You can't provide explicit template arguments for templated constructors - they would have to be deduced. You are probably looking for something like

template <typename Tdst>
template <typename Tsrc>
other<Tdst>::other() : src_((Tsrc*)nullptr, 0) {}
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • Woof, ok, that was the conclusion I was converging on, too bad there's no explicit syntax for it... – gct Aug 07 '18 at 14:50
  • 1
    How does the compiler deduce `Tsrc` from this? – NathanOliver Aug 07 '18 at 14:51
  • @NathanOliver Since the constructor for memsrc is a function template, it's allowed to deduce template parameters, so by giving an explicit type to the first argument, then it can work backwards to Tsrc. – gct Aug 07 '18 at 14:52
  • @SeanMcAllister But what is `Tsrc` in the above code? There is no way to deduce it and you can't explicitly provide a template parameter when call then constructor. – NathanOliver Aug 07 '18 at 14:53
  • @NathanOliver Yes, the next question is, how exactly is one going to construct `other`. – Igor Tandetnik Aug 07 '18 at 14:55
  • @IgorTandetnik Ah I see, yes I elided a little bit to ask the question, I do have a Tsrc going into other as well. – gct Aug 07 '18 at 15:13