0

I do this syntax for myself as I think it is more convenience for later.

temolate<typename U> struct identity{typedef U type;};
temolate<typename T, typename identity<T>::type value=0> struct my{
    typedef std::vector<T> vec;
    typedef std::array<T,value> arr;
    typedef std::set<T> set;
    // and so forth
};  

And I use it:

int main(){
    my<int>::vec v;   // okay
    my<int,3>::arr a; // okay
    // and so forth
}

But I also wish to do this syntax:
(specialize of my above)

template<??????????>  // what should I do here?
struct my<?????????>{ // or may be here
    typedef int i;
    typedef float f;
    typedef double d;
    // and so forth;
}

So that I can do this:

int main(){
    my::i a; // for int, what should I do?
    my::f b; // for float,  and
    my::d c; // for double, and
    // AND I ALSI CAN
    my<int>::vec v;    // already I know
    my<int,3>::arr a;  // and know
}

Is it possible?


I have seen here:
Default template parameter partial specialization before I ask. So I know my<>::i is possible.

And I also know how to use alias with using

I JUST ASK it is possible? Instead of saying NO to me, you get me downvote

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44

2 Answers2

4

You can default your T to a special type (here default_type) and then specialize for it:

template<typename U> struct almost_identity{typedef U type;};

class default_type{};

template<> struct almost_identity<default_type>{ typedef int type; };

template<typename T = class default_type, typename almost_identity<T>::type value = 0> struct my{
    typedef std::vector<T> vec;
    typedef std::array<T,value> arr;
    typedef std::set<T> set;
    // and so forth
};

template<>
struct my<default_type, 0>
{
    typedef int i;
    typedef float f;
    typedef double d;
};

demo

This will allow you to use my<>::i.

If you absolutely want my::i and my<int>::vec to be correct, then as far as I can tell there's no way to accomplish this.

krzaq
  • 16,240
  • 4
  • 46
  • 61
  • compiler says: `error: ‘template::type value> struct my’ used without template parameters` – Shakiba Moshiri Nov 04 '16 at 16:27
  • @k-five where did you get that `U` from? – krzaq Nov 04 '16 at 16:28
  • 2
    @k-five you need to use `my` with template brackets like GillBates suggested i.e. `my<>::i` – W.F. Nov 04 '16 at 16:29
  • @k-five I'm pretty sure this isn't possible without some ugly macros – krzaq Nov 04 '16 at 16:31
  • @k-five well you could use of some `using` as skypjack suggested :) – W.F. Nov 04 '16 at 16:32
  • I before asking saw this: https://stackoverflow.com/questions/13291270/function-template-specialization-with-a-template-class – Shakiba Moshiri Nov 04 '16 at 16:33
  • @W.F. I don't see how, not when you want to allow both `my::i` and `my::vec` syntaxes – krzaq Nov 04 '16 at 16:33
  • @krzaq If it is not possible JUST say no, DO NOT gave my a code – Shakiba Moshiri Nov 04 '16 at 16:34
  • @k-five but this isn't a function template. – krzaq Nov 04 '16 at 16:34
  • @krzaq you're right it might be hard to get both at least in one namespace – W.F. Nov 04 '16 at 16:36
  • @krzaq may be you are right I do not know, as I said , I hope to because it is very clear and beauty – Shakiba Moshiri Nov 04 '16 at 16:39
  • @k-five unfortunately one cannot do type overloads it's syntactic sugar reserved for functions :) – W.F. Nov 04 '16 at 16:41
  • 2
    @k-five I answered the question as asked. I guess you can blame the language barrier. That said, I don't see how this warrants a downvote. – krzaq Nov 04 '16 at 16:45
  • @krzaq As I get a down vote. my answer simply is `no` but ...! – Shakiba Moshiri Nov 04 '16 at 16:53
  • @k-five it's not my down vote but in your question you did not clearly state what is your use case and as such it may be less valuable for future OPs that could have similar question as yours. This does not imply lack of usefulness of skypjack's and krzaq's answers... – W.F. Nov 04 '16 at 17:00
  • @k-five edit your question to make it clearer and I will be first to upvote :) – W.F. Nov 04 '16 at 17:01
  • @W.F., @ krzaq Of course YOUR code is useful, but not for me since I know to use `alias`. I just say it is `possible`? I never WANT someone explain to me the different syntax. I always search before I ask. – Shakiba Moshiri Nov 04 '16 at 17:05
  • 1
    @k-five when you come down read answers and comments again - no one here is trying to bug you but to help you – W.F. Nov 04 '16 at 17:14
1

You you are willing to accept a sligtly different name, you can use an using declaration to do something similar:

using myy = my<int>;

Then refer to your type as:

myy::i
skypjack
  • 49,335
  • 19
  • 95
  • 187