1

C++ templates are checked at least twice. First, when a template is declared & defined, second when it is instantiated. After a template successfully instantiated it is in a type safe state. My question is that what is the name of the state where a template is in when a template is successfully declared & defined? If I compare macro with templates, is it true that after a successful macro "instantiation" the code is type safe?

#define BAR(x) return x;
                        ^
// BAR is preprocessor-grammar-safe here?

struct Bar
{
  static void bar() {}
};
  ^
// Bar is type safe at this point?

template<typename T>
void foo()
{
  T::bar();
}
 ^
// foo is C++-grammar safe at this point?

int main()
{
  foo<Bar>();
             ^
  // foo is type safe at this point?

  foo<int>();
         ^
  // foo is ill-formed here?

  BAR(0);
         ^
  // BAR is type safe at this point?

  return 0;
}
  • I am not sure that *grammar safe* has a precise meaning, and I believe the answer to your question could be compiler specific.... – Basile Starynkevitch Dec 17 '13 at 19:03
  • @BasileStarynkevitch Said the compiler developer guy :) –  Dec 17 '13 at 19:05
  • That's a loaded question. You say a lot of things that sound like words, but don't actually mean anything. As such it's hard to respond do that. – Kerrek SB Dec 17 '13 at 19:09
  • @Kerrek SB: Sorry for my English. My problem is that, foo is not type safe after it is successfully declared, only "grammar safe" or "gramatically correct". What is the correct name for that state? – Industrial-antidepressant Dec 17 '13 at 19:15
  • I don't believe that your claim that "foo is not type safe" makes any sense. "Type safe" just doesn't mean what you think, or at least you're not using it in a way that is common. – Kerrek SB Dec 17 '13 at 19:16
  • Maybe "syntax-checked"? – Igor R. Dec 17 '13 at 19:19
  • @Kerrek SB: What is the correct terminology? ex.: "Templates are considered type-safe; that is, they require type-checking at compile time." http://en.wikipedia.org/wiki/Template_%28C++%29 http://stackoverflow.com/questions/17970627/why-are-c-macros-not-type-safe – Industrial-antidepressant Dec 17 '13 at 19:19
  • You tell me. I don't the the Wikipedia article that's aimed at a general computer-science interested audience is useful here to get a deep understanding of C++. It may be better for you to *first* understand the opposite, e.g. why `printf` is type-*unsafe*. – Kerrek SB Dec 17 '13 at 19:25
  • @Kerrek SB: printf is really type-unsafe at C++ level? – Industrial-antidepressant Dec 17 '13 at 19:33
  • "Type safe" generally means that the compiler is able to proactively check to ensure variables and functions can be legally invoked. Ex. class Test has a method called bar() but not foo(). The compiler will reject a program where a call to foo is attempted. Non-type safety implies that these sort of checks only occur at run time and typically fail with an exception. – seand Dec 17 '13 at 22:48

3 Answers3

3

A template is a template.

A type is a type.

Types may be obtained by specializing templates ("instantiating" in colloquial speech).

Templates and types have names. Types that are unions and classes must be declared, and defined in order to be complete. Templates must be declared, and defined in order to specialized.

That's all I can think of, and that should be enough vocabulary to talk about most aspects of the type system of C++ on a "first draft" kind of level.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

Think of a C++ template as a macro on steroids and having syntactical knowledge. ex.

template<typename T> 
void func(T t) {
  t.foo();
  ...

Nothing declares the fact that T must have a foo() function; This distinguishes C++ templates from things like Scala type parameters. I believe newer C++ versions have added some support for this. You basically let the compile "try" using the t.foo() and if there's no such function it fails to compile.

seand
  • 5,168
  • 1
  • 24
  • 37
1

You just miss a little point: Macro processing is text processing (done before compilation)

Assuming preprocessing has generated valid code: A template can be successfully declared. Instantiation (definition) might fail, however.

Essentially BAR(0); is not type safe (exaggerating a bit here) !