Template question

A

Adrian

Hi All,

Is the output from the following code correct? Is the result
undefined? I am not quite sure what I expected from this :). Should
both the time_t and DateTime creation call the specialized class or
should only the time_t. Both results would make sense to me.

Does it say this anywhere in the standard at all?

TIA

Adrian

------------------------------------
#include <iostream>
#include <ctime>

typedef time_t DateTime;

class BoringBase
{
public:
virtual ~BoringBase() {}
};

template<typename T>
class Foo : public BoringBase
{
public:
Foo() { std::cout << "Constructing run-of-the-mill Foo\n"; }
};

template<>
class Foo<time_t> : public BoringBase
{
public:
Foo() { std::cout << "Special time_t Foo\n"; }
};

int main(int /* argc */, char /* *argv[] */)
{
BoringBase *a,*b,*c;

a=new Foo<int>();
b=new Foo<time_t>();
c=new Foo<DateTime>();

return 0;
}

// Outputs:
// [adrianc@mlx32dev:~]$g++ -Wall -ansi -pedantic -Wextra -Weffc++
foo.cc
// [adrianc@mlx32dev:~]$a.out
// Constructing run-of-the-mill Foo
// Special time_t Foo
// Special time_t Foo
 
G

Gert-Jan de Vos

Hi All,

Is the output from the following code correct? Is the result
undefined? I am not quite sure what I expected from this :). Should
both the time_t and DateTime creation call the specialized class or
should only the time_t. Both results would make sense to me.

Does it say this anywhere in the standard at all?

TIA

Adrian

------------------------------------
#include <iostream>
#include <ctime>

typedef time_t DateTime;

class BoringBase
{
   public:
      virtual ~BoringBase() {}

};

template<typename T>
class Foo : public BoringBase
{
   public:
      Foo() { std::cout << "Constructing run-of-the-mill Foo\n"; }

};

template<>
class Foo<time_t> : public BoringBase
{
   public:
      Foo() { std::cout << "Special time_t Foo\n"; }

};

int main(int /* argc */, char /* *argv[] */)
{
   BoringBase *a,*b,*c;

   a=new Foo<int>();
   b=new Foo<time_t>();
   c=new Foo<DateTime>();

   return 0;

}

// Outputs:
// [adrianc@mlx32dev:~]$g++ -Wall -ansi -pedantic -Wextra -Weffc++
foo.cc
// [adrianc@mlx32dev:~]$a.out
// Constructing run-of-the-mill Foo
// Special time_t Foo
// Special time_t Foo

This result is correct. The typedef does not create a new type, it
defines a synonym for time_t named DateTime. Both are names for one
and the same type, therefore both b and c show the same behavior. Use
struct, class, union or enum to define new types.
 
W

werasm

Hi All,

Is the output from the following code correct? Is the result
undefined? I am not quite sure what I expected from this :). Should
both the time_t and DateTime creation call the specialized class or
should only the time_t. Both results would make sense to me.

The output is correct. The typedef just provides an alias (or a
synonym) for the type, it does not introduce a new type[7.1.3.1].

Regards,

Werner
 
V

Victor Bazarov

Adrian said:
Is the output from the following code correct?

You mean, is it OK that when you instantiate Foo<DateTime> it actually
instantiates Foo<time_t>? 'DateTime' in your program is *the synonym*
for 'time_t'. Almost like a macro. When you write 'DateTime', the
compiler actually uses 'time_t' *because you told it to*.
> Is the result
undefined?

Why would it be undefined?
> I am not quite sure what I expected from this :).

What do you mean, exactly? How can you not be sure what you expected?
Perhaps you have forgotten what you expected, but you did expect
something specific when you were writing the code, no?
> Should
both the time_t and DateTime creation call the specialized class or
should only the time_t. Both results would make sense to me.

How can different results both make sense? It's not like the behaviour
is implementation-specific. It's very well defined.
Does it say this anywhere in the standard at all?

Not sure what your confusion is. If you declare

time_t t1;
DateTime t2; // considering 'DateTime' is your typedef

do you expect the types of 't1' and 't2' to be the same or different?
What if you have a function:

void bar(time_t);

and you call it

bar(t2);

do you expect it to work or do you expect a compiler error? If the
latter, what would be the cause of the error? If the former, and the
types are fully equivalent, why would they be different in case of
templates?
TIA

Adrian

------------------------------------
#include <iostream>
#include <ctime>

typedef time_t DateTime;

class BoringBase
{
public:
virtual ~BoringBase() {}
};

template<typename T>
class Foo : public BoringBase
{
public:
Foo() { std::cout << "Constructing run-of-the-mill Foo\n"; }
};

template<>
class Foo<time_t> : public BoringBase
{
public:
Foo() { std::cout << "Special time_t Foo\n"; }
};

int main(int /* argc */, char /* *argv[] */)
{
BoringBase *a,*b,*c;

a=new Foo<int>();
b=new Foo<time_t>();
c=new Foo<DateTime>();

return 0;
}

// Outputs:
// [adrianc@mlx32dev:~]$g++ -Wall -ansi -pedantic -Wextra -Weffc++
foo.cc
// [adrianc@mlx32dev:~]$a.out
// Constructing run-of-the-mill Foo
// Special time_t Foo
// Special time_t Foo

V
 
A

Adrian

This result is correct. The typedef does not create a new type, it
defines a synonym for time_t named DateTime. Both are names for one
and the same type, therefore both b and c show the same behavior. Use
struct, class, union or enum to define new types.

Thanks - that makes sense
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top