5

I have to implement basically the same function but for different size. Specifically it is something like...

type& operator=(unsigned int);
type& operator=(unsigned long int);
type& operator=(unsigned long long int);
type& operator=(int);
type& operator=(long int);
type& operator=(long long int);
type& operator=(short int);
//so on and so forth...

They have to do exactly the same thing... (except i should take into account the different size), the main idea would be "if the type is the widest use the code for the task... otherwise perform a casting and execute the code". Is it possible to avoid all such repetitive code by using only one method? (i just don't want the compiler throw me some warning when i compile...).

Thank you

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • Assignment from primitive integer types is problematic for various reasons, but if you must do it: have you tried just implementing the overloads for `intmax_t` and `uintmax_t` (from ``)? – pmdj Sep 23 '15 at 10:36
  • Yes i'm using cstdint, i just want to avoid to write all the same code... in elegant and efficient way. – user8469759 Sep 23 '15 at 10:39

2 Answers2

6

This will work for all integral types:

template<
    typename T,
    typename = std::enable_if_t<std::is_integral<T>::value>
>
type& operator= (T);

If you want to depend on size, it can be obtained with simply sizeof(T). You can compare that with your biggest desired size.

If you wanted two separate functions, you'd need to put that clause in as well and use some sort of static-all.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Is there any alternative to templates? they're pretty hard to read. – user8469759 Sep 23 '15 at 10:40
  • @Lukkio Excuse me, but what do you find hard to read in this particular code? I think it's pretty straightforward. – Bartek Banachewicz Sep 23 '15 at 10:40
  • Could you forward me some reference on template syntax? My max in using template is template or i specify a specific type for T. – user8469759 Sep 23 '15 at 10:45
  • @Lukkio [This link](http://loungecpp.wikidot.com/tips-and-tricks:enable-if-for-c-11) might help, as well as [`type_traits`](http://en.cppreference.com/w/cpp/header/type_traits) documentation. – Bartek Banachewicz Sep 23 '15 at 10:47
  • @BartekBanachewicz `typename std::enable_if::type* = nullptr` should work. – TartanLlama Sep 23 '15 at 10:47
  • Or you could just have another overload which would be preferred to the template – TartanLlama Sep 23 '15 at 10:50
  • @TartanLlama what do you mean? – user8469759 Sep 23 '15 at 10:53
  • @BartekBanachewicz your use of template, your specific use i mean, is a feature of C++11, isn't? – user8469759 Sep 23 '15 at 10:56
  • @Lukkio `enable_if_t` was added in C++14. – Bartek Banachewicz Sep 23 '15 at 10:56
  • @BartekBanachewicz i don't understand the semantic of the syntax you've used. If i putted template it means i'm parametrizing two types what have you putted in the second argument of your template is a kind a proposition without a name i can't understand how to intepret it (but the idea is clear). – user8469759 Sep 23 '15 at 11:12
  • you can drop the `typename enable_if_t` since the `_t` already extracts the `::type` – TemplateRex Sep 23 '15 at 11:16
  • @Lukkio this is just a way to put that clause in. SFINAE requires the function type to contain the check as either template parameter, return type or a parameter type. Either would work, I just prefer this form. – Bartek Banachewicz Sep 23 '15 at 11:17
  • 2
    I prefer the anser by @SimonKraemer because it allows multiple overloads. Using SFINAE on default function template arguments is only for cases where you want to restrict to a single subset. – TemplateRex Sep 23 '15 at 11:21
  • @BartekBanachewicz so actually your code (very informally) should mean "apply the template if T is of integral type", isn't? you defined a parameter in the first argument, on the second you defined a kind of constraint on it ,is it? – user8469759 Sep 23 '15 at 11:47
  • would be possible to add a further condition that check the type is signed or not? if it is signed i would like to generate some code (the templates are a kind of macro afaik) otherwise generate other code. – user8469759 Sep 23 '15 at 11:48
  • @Lukkio yes, it is possible to check if the type is signed or not using something like `std::is_signed`. Please ask another question if you have issues implementing this. – TartanLlama Sep 23 '15 at 12:58
6

In addition to the answer of Bartek Banachewicz an alternative way to solve it:

struct type
{
    ////Solution by Bartek Banachewicz
    //template<typename T,typename = typename std::enable_if<std::is_integral<T>::value>::type>
    //type& operator= (T) 
    //{ 
    //  return *this; 
    //}

    template<typename T>
    auto operator=(T) -> typename std::enable_if<std::is_integral<T>::value, type>::type&
    {
        return *this;
    }
};
Community
  • 1
  • 1
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
  • 1
    I feel that this link http://stackoverflow.com/a/22515589/1614219 might be useful to those who (like me), don't know about the `->` syntax in signatures. – dureuill Sep 23 '15 at 10:50
  • this answer is better because it allows multiple `std::enable_if` overloads (you can't change the default function template argument ) – TemplateRex Sep 23 '15 at 11:20