Topic: Templates as default arguments


Author: dps <servisster@gmail.com>
Date: Fri, 2 Oct 2009 15:21:37 CST
Raw View
Hi,

I have the following situation:

template<typename T, int i> class A
{
     A(){}
};

class B
{
public:
     B(A<double,3> arg=A<double,3>()){}
}

The above is pseudo-code and compiles on VS but not on gcc. Gcc
complains about the "3" in the default argument. But if I write

class B
{
public:
     typedef A<double,3> AD3;
     B(AD3 arg=AD3()){}

there is no problem. Obviously it has to do with the template
instantiation? But why is only the "3" that creates the problem?

Thanks a lot!
}

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Prasoon <prasoonthegreat@gmail.com>
Date: Sat, 3 Oct 2009 20:46:50 CST
Raw View
Default constructor of class A is private.

This might work.
template<typename T, int i> class A
{
  public:
   A(){}
};

class B
{
public:
    B(A<double,3> arg=A<double,3>()){}
} ;

The above is working fine on my g++ 4.4 compiler :)

--

Prasoon

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Paul Bibbings <paul_bibbings@googlemail.com>
Date: Sat, 3 Oct 2009 20:45:32 CST
Raw View
dps wrote:
> Hi,
>
> I have the following situation:
>
> template<typename T, int i> class A
> {
>      A(){}
> };
>
> class B
> {
> public:
>      B(A<double,3> arg=A<double,3>()){}
> }

Aside from the fact that in your 'pseudo-code' you declare the constructor of A
to be private, on correcting this I have no problems getting this to compile
with gcc-4.4.1.

Regards

Paul Bibbings

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Florian Weimer <fw@deneb.enyo.de>
Date: Sun, 4 Oct 2009 00:16:30 CST
Raw View
* dps:

> Hi,
>
> I have the following situation:
>
> template<typename T, int i> class A
> {
>      A(){}
> };
>
> class B
> {
> public:
>      B(A<double,3> arg=A<double,3>()){}
> }
>
> The above is pseudo-code and compiles on VS but not on gcc.

With struct instead of class and an additional semicolon, it compiles
with GCC 4.4.  It's presumably a bug in earlier versions.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 4 Oct 2009 00:16:30 CST
Raw View
dps wrote:

> Hi,
>
> I have the following situation:
>
> template<typename T, int i> class A
> {
>      A(){}
> };
>
> class B
> {
> public:
>      B(A<double,3> arg=A<double,3>()){}
> }
>
> The above is pseudo-code and compiles on VS but not on gcc. Gcc
> complains about the "3" in the default argument. But if I write
>
> class B
> {
> public:
>      typedef A<double,3> AD3;
>      B(AD3 arg=AD3()){}
>
> there is no problem. Obviously it has to do with the template
> instantiation? But why is only the "3" that creates the problem?
>

That looks like an instance of this issue report: http://www.open-
std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 which has a GCC bug report
too http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sun, 4 Oct 2009 02:37:32 CST
Raw View
On 2 Okt., 23:21, dps <serviss...@gmail.com> wrote:
> Hi,
>
> I have the following situation:
>
> template<typename T, int i> class A
> {
>      A(){}
>
> };
>
> class B
> {
> public:
>      B(A<double,3> arg=A<double,3>()){}
>
> }
>
> The above is pseudo-code and compiles on VS but not on gcc. Gcc
> complains about the "3" in the default argument. But if I write
>
> class B
> {
> public:
>      typedef A<double,3> AD3;
>      B(AD3 arg=AD3()){}
>
> there is no problem. Obviously it has to do with the template
> instantiation? But why is only the "3" that creates the problem?

I don't believe that the "3" is the problem. The example
you provided is ill-formed (e.g. private default constructor) and
I don't recommend to provide a "pseudo-code" for a situation
that causes a problem, because this has the fundamental
tendency to hide the actual problem. Pseudo-code makes
sense if you want to present a new idea, because it emphasizes
your *idea* what the code should do, but is a bad choice for
to present a situation which causes a compile or runtime problem,
because to solve this one isn't primarily interested to get your
idea, but to understand the nature of the problem.

It would also be helpful, if you have provided us information
about the compiler versions. From what you present here
it looks like a compiler error. If the default constructor was
actually public and the class definition of B is completed with
a semicolon and the code still causes a compiler error, this is
a clearly a compiler defect.

HTH && Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: dps <servisster@gmail.com>
Date: Mon, 5 Oct 2009 12:12:26 CST
Raw View
On Oct 4, 8:16 am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:

> That looks like an instance of this issue report:http://www.open-
> std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 which has a GCC bug report
> toohttp://gcc.gnu.org/bugzilla/show_bug.cgi?id=57

Hi Johannes,

that makes sense, I went through the standard and did not find a clear
definition for this myself, so it probably is unclear how to parse
this. I didn't think it could be a compiler problem, we are bound to
use GCC 4.1.2 for this project. Thanks a lot for pointing me to the
right direction!

Best regards,

Dimitris Servis


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]