What is the template syntax for an arbitrary container of doubles? [duplicate]
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?
c++ c++11 templates
marked as duplicate by πάντα ῥεῖ
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.
add a comment |
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?
c++ c++11 templates
marked as duplicate by πάντα ῥεῖ
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 abouty, just writefor (double & d : y)
– Caleth
Nov 23 '18 at 12:30
add a comment |
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?
c++ c++11 templates
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
c++ c++11 templates
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 πάντα ῥεῖ
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 πάντα ῥεῖ
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 abouty, just writefor (double & d : y)
– Caleth
Nov 23 '18 at 12:30
add a comment |
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 abouty, just writefor (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
add a comment |
1 Answer
1
active
oldest
votes
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
the OP specifically asked to restrict the use to only containers ofdouble. Your code has no problem to compile forstd::vector<int>.
– bolov
Nov 23 '18 at 11:59
It is nonetheless an elegant solution. The OP could always static_assert onTif they insisted.
– Bathsheba
Nov 23 '18 at 12:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
the OP specifically asked to restrict the use to only containers ofdouble. Your code has no problem to compile forstd::vector<int>.
– bolov
Nov 23 '18 at 11:59
It is nonetheless an elegant solution. The OP could always static_assert onTif they insisted.
– Bathsheba
Nov 23 '18 at 12:01
add a comment |
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
the OP specifically asked to restrict the use to only containers ofdouble. Your code has no problem to compile forstd::vector<int>.
– bolov
Nov 23 '18 at 11:59
It is nonetheless an elegant solution. The OP could always static_assert onTif they insisted.
– Bathsheba
Nov 23 '18 at 12:01
add a comment |
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
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
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 ofdouble. Your code has no problem to compile forstd::vector<int>.
– bolov
Nov 23 '18 at 11:59
It is nonetheless an elegant solution. The OP could always static_assert onTif they insisted.
– Bathsheba
Nov 23 '18 at 12:01
add a comment |
the OP specifically asked to restrict the use to only containers ofdouble. Your code has no problem to compile forstd::vector<int>.
– bolov
Nov 23 '18 at 11:59
It is nonetheless an elegant solution. The OP could always static_assert onTif 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
add a comment |
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 writefor (double & d : y)– Caleth
Nov 23 '18 at 12:30