Can I use constrained-type-specifier in a body of abbreviated function template?

58 views
Skip to first unread message

Anton Bikineev

unread,
Jun 8, 2016, 11:17:27 PM6/8/16
to SG8 - Concepts
Hi SG8,

I'm particularly interested in forwarding (or how to achieve it) reference from abbreviated function template, e.g.

void foo(concept<T>&& c)
{
   bar
(std::forward<concept<T>>(c));
}

Thanks!

Tom Honermann

unread,
Jun 9, 2016, 12:41:37 AM6/9/16
to conc...@isocpp.org
Use decltype:

void foo(concept<T>&& c)
{

   bar
(std::forward<decltype(c)>(c));
}

Tom.

Anton Bikineev

unread,
Jun 9, 2016, 6:13:54 AM6/9/16
to SG8 - Concepts
Thanks! I thought that std::forward expects either T or T& as a template argument. It turns out that with help of reference collapsing its returned type forwards correctly.
But is it really possible in the current TS to use constrained type specifier to declare local variables inside a body of abbreviated function template?

четверг, 9 июня 2016 г., 7:41:37 UTC+3 пользователь Tom Honermann написал:

Andrew Sutton

unread,
Jun 9, 2016, 8:29:13 AM6/9/16
to conc...@isocpp.org

But is it really possible in the current TS to use constrained type specifier to declare local variables inside a body of abbreviated function template?


Yes, it is.
 
--
Andrew Sutton
Message has been deleted

Anton Bikineev

unread,
Jun 9, 2016, 8:46:18 AM6/9/16
to SG8 - Concepts
So, why doesn't this code work as expected? is it a gcc bug or me doing something wrong?

template <class T, class U>
concept bool some_concept = true;

void foo(some_concept<int>)
{
    some_concept
<int> a = 0;
}

struct A
{
    A
(int a){std::cout << __PRETTY_FUNCTION__<< ": " << a << std::endl;}
};

int main()
{
    A a
(3);
    foo
(a); // the program prints only one line: "A::A(int): 3"
}

Thanks.

четверг, 9 июня 2016 г., 15:29:13 UTC+3 пользователь Andrew Sutton написал:

Andrew Sutton

unread,
Jun 9, 2016, 10:39:41 AM6/9/16
to SG8 - Concepts
There's no relation between the type of the parameter and the type deduced for the variable declaration. In your example, the type of a is deduced from its initializer, so it becomes int.


--
You received this message because you are subscribed to the Google Groups "SG8 - Concepts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to concepts+u...@isocpp.org.
To post to this group, send email to conc...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/concepts/.
--
Andrew Sutton

Tony V E

unread,
Jun 9, 2016, 10:53:28 AM6/9/16
to SG8 - Concepts
But these are still the same type:

void foo(some_concept<int> a, some_concept<int> b)
{
    some_concept<int> c = 0;
}

So a and b are the same type, but c isn't?

Or did we finally change that?

Sent from my BlackBerry portable Babbage Device
From: Andrew Sutton
Sent: Thursday, June 9, 2016 10:39 AM
To: SG8 - Concepts
Subject: Re: [concepts] Can I use constrained-type-specifier in a body of abbreviated function template?

Ville Voutilainen

unread,
Jun 9, 2016, 11:13:53 AM6/9/16
to conc...@isocpp.org
On 9 June 2016 at 17:53, Tony V E <tvan...@gmail.com> wrote:
But these are still the same type:

void foo(some_concept<int> a, some_concept<int> b)
{
    some_concept<int> c = 0;
}

So a and b are the same type, but c isn't?

Correct.
 

Or did we finally change that?

I don't know what you mean by "finally".

Andrew Sutton

unread,
Jun 9, 2016, 11:14:38 AM6/9/16
to conc...@isocpp.org
void foo(some_concept<int> a, some_concept<int> b)
{
    some_concept<int> c = 0;
}

So a and b are the same type, but c isn't?
 
For better or worse, yes. 

There are compelling reasons for making a and b have the same type, especially if you want definition checking. There are fewer compelling reasons to make c also have that same type.

--
Andrew Sutton

Tom Honermann

unread,
Jun 9, 2016, 11:30:13 AM6/9/16
to conc...@isocpp.org
Despite those compelling reasons, I still find that behavior surprising and another reason why I oppose the abbreviated function template syntax.

Tom.
Reply all
Reply to author
Forward
0 new messages