|
-
September 27th, 2010, 08:52 PM
#1
Get decltype to deduce member variable constness
Consider the following code:
Code:
template <typename T>
class really_long_complicated_typename
{
public:
really_long_complicated_typename(T& x) : x(x) {}
private:
T& x;
};
template <typename T>
really_long_complicated_typename<T> f(T& t)
{
return really_long_complicated_typename<T>(t);
}
class A
{
class C {};
C c;
public:
A() {}
decltype(f(c)) get_c() const { return f(c); }
};
int main()
{
const A a;
a.get_c();
}
g++ gives the error:
Code:
test.cpp: In member function really_long_complicated_typename<A::C> A::get_c() const:
test.cpp:24: error: conversion from really_long_complicated_typename<const A::C> to non-scalar type really_long_complicated_typename<A::C> requested
The problem is that the expression f(c) in the return statement is really_long_complicated_typename<const A::C>, but decltype doesn't "know" that this is a const member function, so decltype(f(c)) is deduced to be really_long_complicated_typename<A::C>.
Is there a way around this?
Old Unix programmers never die, they just mv to /dev/null
-
September 28th, 2010, 03:47 AM
#2
Re: Get decltype to deduce member variable constness
have you tried "auto get_c() const -> decltype(f(c)) { return f(c); }" or the more explicit "auto get_c() const -> decltype(f( this->c )) { return f(c); }" instead ?
Last edited by superbonzo; September 28th, 2010 at 03:57 AM.
-
September 28th, 2010, 11:21 AM
#3
Re: Get decltype to deduce member variable constness
 Originally Posted by superbonzo
have you tried "auto get_c() const -> decltype(f(c)) { return f(c); }" or the more explicit "auto get_c() const -> decltype(f( this->c )) { return f(c); }" instead ?
The first makes no difference.
The second gives the error "invalid use of 'this' at top level".
Perhaps it's just my compiler that doesn't support this feature fully? I am using g++ 4.4.
Old Unix programmers never die, they just mv to /dev/null
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|