Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22001

Returning new object from function defined using Concepts

$
0
0

Code

#include <type_traits>

template <typename N>
concept Number = std::is_arithmetic<N>::value;

template <typename T>
concept VectorXY = requires(T t)
{
    {t.x} -> Number;
    {t.y} -> Number;
};

template <Number N>
struct Vec2
{
    N x = 0;
    N y = 0;
};

VectorXY operator*(VectorXY v, Number n)
{
    return {v.x * n, v.y * n};
    // error: returning initializer list
}

int main()
{
    Vec2<float> v = Vec2<float>{} * 1;
    // error: conversion from 'void' to non-scalar type 'Vec2<float>' requested
}

Godbolt: https://godbolt.org/z/gYsQ5B


So how do I solve this?

An explanation as to why the compiler can't infer the return type would be helpful as well.


Viewing all articles
Browse latest Browse all 22001

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>