What is the template syntax for an arbitrary container of doubles? [duplicate]












0
















This question already has an answer here:




  • What are some uses of template template parameters?

    9 answers




I have a function that rounds a value according to a policy



double round(double f, Policy p);


What I want to do now is to build a version of this that can be applied only to a container of doubles (there's no point in having a container of any other type due to the way the rounding works).



template <class Iterable>
Iterable<double> round(
Iterable<double> y, Policy p){
for (auto&& e : y){
e = round(e, p);
}
return y;
}


I know my template syntax is not correct, but what should it be?










share|improve this question















marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Here is your answer, I think, stackoverflow.com/questions/213761/…

    – luk32
    Nov 23 '18 at 11:51













  • You don't have to specify anything about y, just write for (double & d : y)

    – Caleth
    Nov 23 '18 at 12:30
















0
















This question already has an answer here:




  • What are some uses of template template parameters?

    9 answers




I have a function that rounds a value according to a policy



double round(double f, Policy p);


What I want to do now is to build a version of this that can be applied only to a container of doubles (there's no point in having a container of any other type due to the way the rounding works).



template <class Iterable>
Iterable<double> round(
Iterable<double> y, Policy p){
for (auto&& e : y){
e = round(e, p);
}
return y;
}


I know my template syntax is not correct, but what should it be?










share|improve this question















marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Here is your answer, I think, stackoverflow.com/questions/213761/…

    – luk32
    Nov 23 '18 at 11:51













  • You don't have to specify anything about y, just write for (double & d : y)

    – Caleth
    Nov 23 '18 at 12:30














0












0








0


1







This question already has an answer here:




  • What are some uses of template template parameters?

    9 answers




I have a function that rounds a value according to a policy



double round(double f, Policy p);


What I want to do now is to build a version of this that can be applied only to a container of doubles (there's no point in having a container of any other type due to the way the rounding works).



template <class Iterable>
Iterable<double> round(
Iterable<double> y, Policy p){
for (auto&& e : y){
e = round(e, p);
}
return y;
}


I know my template syntax is not correct, but what should it be?










share|improve this question

















This question already has an answer here:




  • What are some uses of template template parameters?

    9 answers




I have a function that rounds a value according to a policy



double round(double f, Policy p);


What I want to do now is to build a version of this that can be applied only to a container of doubles (there's no point in having a container of any other type due to the way the rounding works).



template <class Iterable>
Iterable<double> round(
Iterable<double> y, Policy p){
for (auto&& e : y){
e = round(e, p);
}
return y;
}


I know my template syntax is not correct, but what should it be?





This question already has an answer here:




  • What are some uses of template template parameters?

    9 answers








c++ c++11 templates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:49







Sasidiran Sangamanautram

















asked Nov 23 '18 at 11:44









Sasidiran SangamanautramSasidiran Sangamanautram

20327




20327




marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 11:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Here is your answer, I think, stackoverflow.com/questions/213761/…

    – luk32
    Nov 23 '18 at 11:51













  • You don't have to specify anything about y, just write for (double & d : y)

    – Caleth
    Nov 23 '18 at 12:30














  • 1





    Here is your answer, I think, stackoverflow.com/questions/213761/…

    – luk32
    Nov 23 '18 at 11:51













  • You don't have to specify anything about y, just write for (double & d : y)

    – Caleth
    Nov 23 '18 at 12:30








1




1





Here is your answer, I think, stackoverflow.com/questions/213761/…

– luk32
Nov 23 '18 at 11:51







Here is your answer, I think, stackoverflow.com/questions/213761/…

– luk32
Nov 23 '18 at 11:51















You don't have to specify anything about y, just write for (double & d : y)

– Caleth
Nov 23 '18 at 12:30





You don't have to specify anything about y, just write for (double & d : y)

– Caleth
Nov 23 '18 at 12:30












1 Answer
1






active

oldest

votes


















3














You need to use template-template parameters:



#include <vector>

template <template <typename...> class Container, typename T>
auto round(Container<T> y){
for (auto&& e : y){
// ...
}
return y;
}

int main()
{
std::vector<double> vec = {1.1, 2.2};
std::vector<double> rounded = round(vec);
}


live on coliru






share|improve this answer
























  • the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

    – bolov
    Nov 23 '18 at 11:59











  • It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

    – Bathsheba
    Nov 23 '18 at 12:01


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You need to use template-template parameters:



#include <vector>

template <template <typename...> class Container, typename T>
auto round(Container<T> y){
for (auto&& e : y){
// ...
}
return y;
}

int main()
{
std::vector<double> vec = {1.1, 2.2};
std::vector<double> rounded = round(vec);
}


live on coliru






share|improve this answer
























  • the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

    – bolov
    Nov 23 '18 at 11:59











  • It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

    – Bathsheba
    Nov 23 '18 at 12:01
















3














You need to use template-template parameters:



#include <vector>

template <template <typename...> class Container, typename T>
auto round(Container<T> y){
for (auto&& e : y){
// ...
}
return y;
}

int main()
{
std::vector<double> vec = {1.1, 2.2};
std::vector<double> rounded = round(vec);
}


live on coliru






share|improve this answer
























  • the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

    – bolov
    Nov 23 '18 at 11:59











  • It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

    – Bathsheba
    Nov 23 '18 at 12:01














3












3








3







You need to use template-template parameters:



#include <vector>

template <template <typename...> class Container, typename T>
auto round(Container<T> y){
for (auto&& e : y){
// ...
}
return y;
}

int main()
{
std::vector<double> vec = {1.1, 2.2};
std::vector<double> rounded = round(vec);
}


live on coliru






share|improve this answer













You need to use template-template parameters:



#include <vector>

template <template <typename...> class Container, typename T>
auto round(Container<T> y){
for (auto&& e : y){
// ...
}
return y;
}

int main()
{
std::vector<double> vec = {1.1, 2.2};
std::vector<double> rounded = round(vec);
}


live on coliru







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 11:55









m.s.m.s.

12.8k54067




12.8k54067













  • the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

    – bolov
    Nov 23 '18 at 11:59











  • It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

    – Bathsheba
    Nov 23 '18 at 12:01



















  • the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

    – bolov
    Nov 23 '18 at 11:59











  • It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

    – Bathsheba
    Nov 23 '18 at 12:01

















the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

– bolov
Nov 23 '18 at 11:59





the OP specifically asked to restrict the use to only containers of double. Your code has no problem to compile for std::vector<int>.

– bolov
Nov 23 '18 at 11:59













It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

– Bathsheba
Nov 23 '18 at 12:01





It is nonetheless an elegant solution. The OP could always static_assert on T if they insisted.

– Bathsheba
Nov 23 '18 at 12:01





這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings