Unfortunately desired syntax can't be possible as names that are passed to the template instantiation have to be already defined. In your case myType1
and myType2
wouldn't name anything from compiler point of view. However if you're not insisting on the syntax you've mentioned you could try to use std::enable_if
as follows:
#include <type_traits>
struct A {
struct myType1: std::enable_if<true, int> { }; //std::conditional_typedef<true,int,myType1>; // Performs "typedef int myType1".
struct myType2: std::enable_if<false, int> { }; //std::conditional_typedef<false,int,myType2>; // Does nothing at all.
};
int main() {
A::myType1::type i;
//A::myType2::type i2; // causes error: no type named 'type' in 'A::myType2'
(void)i;
}
[live demo]
Edit:
Yet another way that came to my mind (utilizing using with default template parameter):
#include <type_traits>
struct A {
template <class T = int>
using myType1 = typename std::enable_if<true, T>::type;
template <class T = int>
using myType2 = typename std::enable_if<false, T>::type;
};
int main() {
A::myType1<> i;
//A::myType2<> j;
(void)i;
}
[live demo]
if(...) typedef ...;
? In particular, what should happen with code that uses the typedef if the typedef isn't "performed" ?void
. Would that fit your case ?#ifdef
?