I am creating a std::tuple
equivalent for a union
(instead of a struct
). To do so, I also added a constructor template, where the first template argument is size_t idx
, to initize the idx th element of the union
. Moreover, there is another variadic template
to specify what the argments of the actual type constructor are.
Unfortunately, I can't seem to specify the idx
template argument when calling the constructor, and it is also not implied (as it is not part of the argument list). Is there any way around this? How can I specify a size_t
contructor template argument?
Example code:
#include <iostream>
template<typename T>
struct Foo
{
T d_val;
size_t d_other_val;
template<size_t idx>
Foo(T val)
{
d_val = val;
d_other_val = idx;
}
};
int main() {
Foo<double> f = Foo<4>(2.6);
std::cout << f.d_val << " " << f.d_other_val << '\n';
}
Source: http://ideone.com/UeBvF5
Of course, the 4 is matched on the class template, and not the constructor template. Is this fixable? Note that idx should be a compile time thing, and not a normal constructor argument. Although in this example, that would be the trivial solution.
PS: the problem of course is, that in general constructor templates are implied by the arguments with which the constructor is called. Implicit specification is to the best of my knowledge not possible for the idx template argument.
[temp.arg.explicit]/7
reads:
[ Note: Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. —end note ]
So you have to either pass size_t idx
as a regular parameter, or add it as struct Foo
's template parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.