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 =)










share|improve this question




















  • 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















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 =)










share|improve this question




















  • 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













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 =)










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












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;
}





share|improve this answer





















  • 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











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',
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
});


}
});














draft saved

draft discarded


















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

























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;
}





share|improve this answer





















  • 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















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;
}





share|improve this answer





















  • 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













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;
}





share|improve this answer












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;
}






share|improve this answer












share|improve this answer



share|improve this answer










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 to std::conditional_t and not add the typename 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










  • @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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine