John P - 1 year ago 57

C++ Question

I have written the following code to infix a templated class:

`template<template<typename...> class C, typename A, typename B>`

struct infix_t

{

typedef C<A, B> type;

};

template<template<typename...> class C, class A>

constexpr infix_t<C, A> operator<(A, infix_t<C>)

{

return {};

}

template<template<typename...> class C, typename A, typename B>

constexpr C<A, B> operator>(infix_t<C, A>, B)

{

return {};

}

This allows me to write

`a <same_as> b`

`same_as`

`infix_t<std::is_same>`

`a`

`b`

`template template argument has different template parameters...`

`infix_t`

`template argument ... must be a class template or alias template`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

To expand and modify the solution by @Yakk a bit, here's a version that allows compile-time comparison of types

```
#include <type_traits>
template<template<class...> class Op, class... Args>
struct infix {};
template<template<class...> class Op, class L>
constexpr infix<Op, L> operator<(L, infix<Op>)
{ return {}; }
template<template<class...> class Op, class L, class R>
constexpr Op<L, R> operator>(infix<Op, L>, R)
{ return {}; }
constexpr auto same_as = infix<std::is_same>{};
template<int N>
constexpr auto int_c = std::integral_constant<int, N>::value;
int main()
{
static_assert( int_c<0> <same_as> int_c<0> );
static_assert( !(int{} <same_as> char{}) );
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**