1
typedef int ONE;
typedef int TWO;
typedef int THREE;

template<typename T>
class SomeTemplate {};

SomeTemplate<TWO> foo;
SomeTemplate<THREE> bar

введите сюда описание изображения

Собственно проблема на изображении, т.е. имеются две переменные использующие шаблон, но тип выводящая IDE является типом первой переменной которая использует такой же шаблон с "одинаковым" typedef. Да, типы одинаковые (указывают на int), но не удобно работать когда их много. Так например я использую библиотеку Vulkan API. Там все через typedef (typedef uint64_t xxxnamexxx). Проверял на Visual Studio 2013 и 2017. Это баг Visual Studio или фича такая и можно ли как то исправить?

4
  • Я бы сказал, что всё логично. Возможно студия пишет имя шаблона по первому найденому, т.к. что для TWO, что для THREE шаблон один.
    – Unick
    11 сен 2017 в 13:42
  • Если есть желание сделать ONE, TWO, THREE действительно разными типами - возможно стоит подумать об использовании fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces 11 сен 2017 в 13:52
  • @Unick Да, но повторюсь опять же, не удобно проверять тип когда таких переменных объявлено много. Допустим 1000 строк кода, а то и больше, придется "листать" код до объявления той самой переменной что бы только узнать тип шаблона потому что IDE слишком логична. Просто от того какой тип переменной - зависит будущий код. Компилятору то все равно какой "алиас" используется, но так снижается удобочитаемость. 11 сен 2017 в 13:52
  • Пишите в MS, это очевидно ошибка. В 2017 студии Help->Send Feedback->Report a problem
    – ixSci
    11 сен 2017 в 14:38

1 ответ 1

0

Судя по всему если вы используете boost то можете использовать BOOST_STRONG_TYPEDEF.

BOOST_STRONG_TYPEDEF(int, ONE)

Если не используете boost, то может помочь слудующий код:

    namespace detail {
    template <typename T> class empty_base {};
}

template <class T, class U, class B = ::detail::empty_base<T> >
struct less_than_comparable2 : B
{
     friend bool operator<=(const T& x, const U& y) { return !(x > y); }
     friend bool operator>=(const T& x, const U& y) { return !(x < y); }
     friend bool operator>(const U& x, const T& y)  { return y < x; }
     friend bool operator<(const U& x, const T& y)  { return y > x; }
     friend bool operator<=(const U& x, const T& y) { return !(y < x); }
     friend bool operator>=(const U& x, const T& y) { return !(y > x); }
};

template <class T, class B = ::detail::empty_base<T> >
struct less_than_comparable1 : B
{
     friend bool operator>(const T& x, const T& y)  { return y < x; }
     friend bool operator<=(const T& x, const T& y) { return !(y < x); }
     friend bool operator>=(const T& x, const T& y) { return !(x < y); }
};

template <class T, class U, class B = ::detail::empty_base<T> >
struct equality_comparable2 : B
{
     friend bool operator==(const U& y, const T& x) { return x == y; }
     friend bool operator!=(const U& y, const T& x) { return !(x == y); }
     friend bool operator!=(const T& y, const U& x) { return !(y == x); }
};

template <class T, class B = ::detail::empty_base<T> >
struct equality_comparable1 : B
{
     friend bool operator!=(const T& x, const T& y) { return !(x == y); }
};

template <class T, class U, class B = ::detail::empty_base<T> >
struct totally_ordered2
    : less_than_comparable2<T, U
    , equality_comparable2<T, U, B
      > > {};

template <class T, class B = ::detail::empty_base<T> >
struct totally_ordered1
    : less_than_comparable1<T
    , equality_comparable1<T, B
      > > {};

#define SAFE_TYPEDEF(T, D)                                      \
struct D                                                        \
    : totally_ordered1< D                                       \
    , totally_ordered2< D, T                                    \
    > >                                                         \
{                                                               \
    T t;                                                        \
    explicit D(const T& t_) : t(t_) {};                         \
    explicit D(T&& t_) : t(std::move(t_)) {};                   \ 
    D() = default;                                              \
    D(const D & t_) = default;                                  \
    D(D&&) = default;                                           \
    D & operator=(const D & rhs) = default;                     \
    D & operator=(D&&) = default;                               \
    operator T & () { return t; }                               \
    bool operator==(const D & rhs) const { return t == rhs.t; } \
    bool operator<(const D & rhs) const { return t < rhs.t; }   \
};

SAFE_TYPEDEF (int, ONE);

Но тогда вам необходимо будет обновить заголовочники Vulkan API.

Похожий вопрос: https://stackoverflow.com/questions/34287842/c-strongly-typed-using-and-typedef

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.