Template function accepting a template class
I have a few aggreagator classes that are supposed to aggregate over a list of variables, for instance:
template<class T>
class Min{
public:
T val=0;
void aggregate(T val_){
if(val_ < val){
val = val_;
}
};
Now, I want to have a merge function that merges two aggregators together. It would accept 2 aggregators ,like Min, and their type.
template<class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
And I want to have a template specialization for each aggregator, but abstraction for the inner aggregator type. But I couldn't find the syntax for it. For instance,
template <> // specialization for Agg
template <class T> // abstraction for Agg's type
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
generates an error
error: too many template-parameter-lists
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
^~~~~~~~~~~~~
What is the correct syntax, then?
c++ templates
add a comment |
I have a few aggreagator classes that are supposed to aggregate over a list of variables, for instance:
template<class T>
class Min{
public:
T val=0;
void aggregate(T val_){
if(val_ < val){
val = val_;
}
};
Now, I want to have a merge function that merges two aggregators together. It would accept 2 aggregators ,like Min, and their type.
template<class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
And I want to have a template specialization for each aggregator, but abstraction for the inner aggregator type. But I couldn't find the syntax for it. For instance,
template <> // specialization for Agg
template <class T> // abstraction for Agg's type
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
generates an error
error: too many template-parameter-lists
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
^~~~~~~~~~~~~
What is the correct syntax, then?
c++ templates
add a comment |
I have a few aggreagator classes that are supposed to aggregate over a list of variables, for instance:
template<class T>
class Min{
public:
T val=0;
void aggregate(T val_){
if(val_ < val){
val = val_;
}
};
Now, I want to have a merge function that merges two aggregators together. It would accept 2 aggregators ,like Min, and their type.
template<class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
And I want to have a template specialization for each aggregator, but abstraction for the inner aggregator type. But I couldn't find the syntax for it. For instance,
template <> // specialization for Agg
template <class T> // abstraction for Agg's type
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
generates an error
error: too many template-parameter-lists
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
^~~~~~~~~~~~~
What is the correct syntax, then?
c++ templates
I have a few aggreagator classes that are supposed to aggregate over a list of variables, for instance:
template<class T>
class Min{
public:
T val=0;
void aggregate(T val_){
if(val_ < val){
val = val_;
}
};
Now, I want to have a merge function that merges two aggregators together. It would accept 2 aggregators ,like Min, and their type.
template<class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
And I want to have a template specialization for each aggregator, but abstraction for the inner aggregator type. But I couldn't find the syntax for it. For instance,
template <> // specialization for Agg
template <class T> // abstraction for Agg's type
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
generates an error
error: too many template-parameter-lists
Min<T> merge<Min<T>>(Min<T> agg1, Min<T> agg2){...}
^~~~~~~~~~~~~
What is the correct syntax, then?
c++ templates
c++ templates
edited Dec 30 '18 at 15:12
yoavm448
asked Dec 30 '18 at 15:07
yoavm448yoavm448
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Note that you must use template template syntax here, unlike your original code.
template<template<class> class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
But that is not really needed, considering the rest of this answer.
As for the rest of the question, partial specialization is allowed for template classes, but not for template functions. For functions such as yours, you should use overloading.
template <class T> // abstraction for Agg's type
Min<T> merge(Min<T> agg1, Min<T> agg2) {...}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978714%2ftemplate-function-accepting-a-template-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Note that you must use template template syntax here, unlike your original code.
template<template<class> class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
But that is not really needed, considering the rest of this answer.
As for the rest of the question, partial specialization is allowed for template classes, but not for template functions. For functions such as yours, you should use overloading.
template <class T> // abstraction for Agg's type
Min<T> merge(Min<T> agg1, Min<T> agg2) {...}
add a comment |
Note that you must use template template syntax here, unlike your original code.
template<template<class> class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
But that is not really needed, considering the rest of this answer.
As for the rest of the question, partial specialization is allowed for template classes, but not for template functions. For functions such as yours, you should use overloading.
template <class T> // abstraction for Agg's type
Min<T> merge(Min<T> agg1, Min<T> agg2) {...}
add a comment |
Note that you must use template template syntax here, unlike your original code.
template<template<class> class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
But that is not really needed, considering the rest of this answer.
As for the rest of the question, partial specialization is allowed for template classes, but not for template functions. For functions such as yours, you should use overloading.
template <class T> // abstraction for Agg's type
Min<T> merge(Min<T> agg1, Min<T> agg2) {...}
Note that you must use template template syntax here, unlike your original code.
template<template<class> class Agg, class T>
Agg<T> merge(Agg<T> agg1, Agg<T> agg2);
But that is not really needed, considering the rest of this answer.
As for the rest of the question, partial specialization is allowed for template classes, but not for template functions. For functions such as yours, you should use overloading.
template <class T> // abstraction for Agg's type
Min<T> merge(Min<T> agg1, Min<T> agg2) {...}
answered Dec 30 '18 at 15:28
Michael VekslerMichael Veksler
2,7471515
2,7471515
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978714%2ftemplate-function-accepting-a-template-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown