Type depends on condition in template
up vote
0
down vote
favorite
Can I make a template function with arguments, which types are depends on template argument? (Code below is just to explain, what I want)
#include <complex>
template <bool two>
void foo( (if (two) ? double* : std::complex<double>* >) input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
I thought std::conditional
would help, but I guess, I don't know how to use it correctly here (code below can't be compiled)
#include <type_traits>
#include <complex>
template <bool two>
void foo(std::conditional<two, double*, std::complex<double>* > input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
It would be very nice, if someone find solution without higher than c++11, so I will be able to compile it both in vs2012 and with gcc-6+. But some examples with c++14 or higher will be good experience too.
Thanks =)
c++ c++11 templates
|
show 4 more comments
up vote
0
down vote
favorite
Can I make a template function with arguments, which types are depends on template argument? (Code below is just to explain, what I want)
#include <complex>
template <bool two>
void foo( (if (two) ? double* : std::complex<double>* >) input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
I thought std::conditional
would help, but I guess, I don't know how to use it correctly here (code below can't be compiled)
#include <type_traits>
#include <complex>
template <bool two>
void foo(std::conditional<two, double*, std::complex<double>* > input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
It would be very nice, if someone find solution without higher than c++11, so I will be able to compile it both in vs2012 and with gcc-6+. But some examples with c++14 or higher will be good experience too.
Thanks =)
c++ c++11 templates
4
Why do you need to even do this? Why nottemplate<typename T> void foo(T input, size_t n) { ...}
?
– NathanOliver
May 5 '17 at 16:23
std::conditional
is the right choice. Did you look in the docs to see how to use it? If you don't have access to C++11, you should be able to implement it yourself (it's documented if you need help).
– Rakete1111
May 5 '17 at 16:24
usestd::conditional
like this:std::conditional<some_bool, type_true, type_false>::type
– qxz
May 5 '17 at 16:25
@NathanOliver,two
is used in function body. Type of input depends on bool, so I want to reduce template arguments
– Sklert
May 5 '17 at 16:29
3
std::conditional_t</*..*/>
in C++14,typename std::conditional</*..*/>::type
for C++11.
– Jarod42
May 5 '17 at 16:30
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Can I make a template function with arguments, which types are depends on template argument? (Code below is just to explain, what I want)
#include <complex>
template <bool two>
void foo( (if (two) ? double* : std::complex<double>* >) input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
I thought std::conditional
would help, but I guess, I don't know how to use it correctly here (code below can't be compiled)
#include <type_traits>
#include <complex>
template <bool two>
void foo(std::conditional<two, double*, std::complex<double>* > input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
It would be very nice, if someone find solution without higher than c++11, so I will be able to compile it both in vs2012 and with gcc-6+. But some examples with c++14 or higher will be good experience too.
Thanks =)
c++ c++11 templates
Can I make a template function with arguments, which types are depends on template argument? (Code below is just to explain, what I want)
#include <complex>
template <bool two>
void foo( (if (two) ? double* : std::complex<double>* >) input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
I thought std::conditional
would help, but I guess, I don't know how to use it correctly here (code below can't be compiled)
#include <type_traits>
#include <complex>
template <bool two>
void foo(std::conditional<two, double*, std::complex<double>* > input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
void foo_double(double *input, size_t n){
foo<true>(input,n);
}
void foo_complex (std::complex<double> *input, size_t n){
foo<false>(input,n);
}
It would be very nice, if someone find solution without higher than c++11, so I will be able to compile it both in vs2012 and with gcc-6+. But some examples with c++14 or higher will be good experience too.
Thanks =)
c++ c++11 templates
c++ c++11 templates
edited Nov 11 at 22:15
max66
33.7k63762
33.7k63762
asked May 5 '17 at 16:21
Sklert
177411
177411
4
Why do you need to even do this? Why nottemplate<typename T> void foo(T input, size_t n) { ...}
?
– NathanOliver
May 5 '17 at 16:23
std::conditional
is the right choice. Did you look in the docs to see how to use it? If you don't have access to C++11, you should be able to implement it yourself (it's documented if you need help).
– Rakete1111
May 5 '17 at 16:24
usestd::conditional
like this:std::conditional<some_bool, type_true, type_false>::type
– qxz
May 5 '17 at 16:25
@NathanOliver,two
is used in function body. Type of input depends on bool, so I want to reduce template arguments
– Sklert
May 5 '17 at 16:29
3
std::conditional_t</*..*/>
in C++14,typename std::conditional</*..*/>::type
for C++11.
– Jarod42
May 5 '17 at 16:30
|
show 4 more comments
4
Why do you need to even do this? Why nottemplate<typename T> void foo(T input, size_t n) { ...}
?
– NathanOliver
May 5 '17 at 16:23
std::conditional
is the right choice. Did you look in the docs to see how to use it? If you don't have access to C++11, you should be able to implement it yourself (it's documented if you need help).
– Rakete1111
May 5 '17 at 16:24
usestd::conditional
like this:std::conditional<some_bool, type_true, type_false>::type
– qxz
May 5 '17 at 16:25
@NathanOliver,two
is used in function body. Type of input depends on bool, so I want to reduce template arguments
– Sklert
May 5 '17 at 16:29
3
std::conditional_t</*..*/>
in C++14,typename std::conditional</*..*/>::type
for C++11.
– Jarod42
May 5 '17 at 16:30
4
4
Why do you need to even do this? Why not
template<typename T> void foo(T input, size_t n) { ...}
?– NathanOliver
May 5 '17 at 16:23
Why do you need to even do this? Why not
template<typename T> void foo(T input, size_t n) { ...}
?– NathanOliver
May 5 '17 at 16:23
std::conditional
is the right choice. Did you look in the docs to see how to use it? If you don't have access to C++11, you should be able to implement it yourself (it's documented if you need help).– Rakete1111
May 5 '17 at 16:24
std::conditional
is the right choice. Did you look in the docs to see how to use it? If you don't have access to C++11, you should be able to implement it yourself (it's documented if you need help).– Rakete1111
May 5 '17 at 16:24
use
std::conditional
like this: std::conditional<some_bool, type_true, type_false>::type
– qxz
May 5 '17 at 16:25
use
std::conditional
like this: std::conditional<some_bool, type_true, type_false>::type
– qxz
May 5 '17 at 16:25
@NathanOliver,
two
is used in function body. Type of input depends on bool, so I want to reduce template arguments– Sklert
May 5 '17 at 16:29
@NathanOliver,
two
is used in function body. Type of input depends on bool, so I want to reduce template arguments– Sklert
May 5 '17 at 16:29
3
3
std::conditional_t</*..*/>
in C++14, typename std::conditional</*..*/>::type
for C++11.– Jarod42
May 5 '17 at 16:30
std::conditional_t</*..*/>
in C++14, typename std::conditional</*..*/>::type
for C++11.– Jarod42
May 5 '17 at 16:30
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You only have to add a ::type
and a typename
template <bool two>
// ......*typename*.....................................................*::type*
void foo (typename std::conditional<two, double*, std::complex<double>* >::type input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
@Sklert in C++14, you can instead just change your code tostd::conditional_t
and not add thetypename
and::type
.
– Yakk - Adam Nevraumont
May 5 '17 at 17:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You only have to add a ::type
and a typename
template <bool two>
// ......*typename*.....................................................*::type*
void foo (typename std::conditional<two, double*, std::complex<double>* >::type input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
@Sklert in C++14, you can instead just change your code tostd::conditional_t
and not add thetypename
and::type
.
– Yakk - Adam Nevraumont
May 5 '17 at 17:18
add a comment |
up vote
2
down vote
accepted
You only have to add a ::type
and a typename
template <bool two>
// ......*typename*.....................................................*::type*
void foo (typename std::conditional<two, double*, std::complex<double>* >::type input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
@Sklert in C++14, you can instead just change your code tostd::conditional_t
and not add thetypename
and::type
.
– Yakk - Adam Nevraumont
May 5 '17 at 17:18
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You only have to add a ::type
and a typename
template <bool two>
// ......*typename*.....................................................*::type*
void foo (typename std::conditional<two, double*, std::complex<double>* >::type input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
You only have to add a ::type
and a typename
template <bool two>
// ......*typename*.....................................................*::type*
void foo (typename std::conditional<two, double*, std::complex<double>* >::type input, size_t n)
{
for (size_t i = 0; i < n; ++n)
input[i] *= two ? 2.0 : 1.0;
}
answered May 5 '17 at 16:27
max66
33.7k63762
33.7k63762
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
@Sklert in C++14, you can instead just change your code tostd::conditional_t
and not add thetypename
and::type
.
– Yakk - Adam Nevraumont
May 5 '17 at 17:18
add a comment |
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
@Sklert in C++14, you can instead just change your code tostd::conditional_t
and not add thetypename
and::type
.
– Yakk - Adam Nevraumont
May 5 '17 at 17:18
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
Thank you! It works! I will mark this answer after time limit =)
– Sklert
May 5 '17 at 16:32
@Sklert in C++14, you can instead just change your code to
std::conditional_t
and not add the typename
and ::type
.– Yakk - Adam Nevraumont
May 5 '17 at 17:18
@Sklert in C++14, you can instead just change your code to
std::conditional_t
and not add the typename
and ::type
.– Yakk - Adam Nevraumont
May 5 '17 at 17:18
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f43809629%2ftype-depends-on-condition-in-template%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
4
Why do you need to even do this? Why not
template<typename T> void foo(T input, size_t n) { ...}
?– NathanOliver
May 5 '17 at 16:23
std::conditional
is the right choice. Did you look in the docs to see how to use it? If you don't have access to C++11, you should be able to implement it yourself (it's documented if you need help).– Rakete1111
May 5 '17 at 16:24
use
std::conditional
like this:std::conditional<some_bool, type_true, type_false>::type
– qxz
May 5 '17 at 16:25
@NathanOliver,
two
is used in function body. Type of input depends on bool, so I want to reduce template arguments– Sklert
May 5 '17 at 16:29
3
std::conditional_t</*..*/>
in C++14,typename std::conditional</*..*/>::type
for C++11.– Jarod42
May 5 '17 at 16:30