How to specify the template parameters of a template member of a template class?

Jul 15, 2023 at 11:15pm
Consider the following:

1
2
3
4
5
6
7
8
9
10
template<typename T> class Blob
{
public:
	template<typename It>
	Blob(It b, It e) : data(std::make_shared<std::vector<T>>(b,e)) {}


private:
	std::shared_ptr<std::vector<T>> data;
};


What if It cannot be deduced from the constructors arguments? How do you specify it?

1
2
using T = std::vector<int>::iterator;
Blob<int> blob<T>(v.begin(), v.end());


??
this does not compile!
Jul 16, 2023 at 3:31am
Jul 16, 2023 at 4:40pm
I know the first code compiles.

my question is the following: assume the template parameter It of the constructor wasn't deduced, how could one specify the ctor template??


what does not compile is

1
2
using T = std::vector<int>::iterator;
Blob<int> blob<T>(v.begin(), v.end());


I know in this case the template of the ctor is deduced and there is no problem. My question is ASSUME It WAS NOT deduced, then HOW can we specify it?

Jul 17, 2023 at 12:08am
If the question is "how do I explicitly specify a constructor's template arguments?" the unfortunate answer is "you can't".
[temp.arg.explicit] has this note:
[Note 4: Because the explicit template argument list follows the function template name, and because constructor templates ([class.ctor]) are named without using a function name ([class.qual]), there is no way to provide an explicit template argument list for these function templates.
— end note]

https://eel.is/c++draft/temp#arg.explicit-8
Last edited on Jul 17, 2023 at 12:11am
Jul 17, 2023 at 4:10pm
that's what I was fearing!!

Thank you!!
Topic archived. No new replies allowed.