Clang: template functions

Hi,

For the first time I built llvm + clang on ubuntu box for giving it a try.

I have this simple template function in a header file

template <typename T, typename U>
inline void SetMin(T &aVar,U aValue);

but clang++ returns

…/src/myfile.h:13:1: error: unknown type name ‘template’
template <typename T, typename U>
^
…/src/myfile.h:13:10: error: expected identifier or ‘(’
template <typename T, typename U>

I tried to use also -std=gnu89 because I’ve read gcc (which compiles my code) uses that standard by default, but with no success.

I am using:

clang version 2.9 (trunk 118246)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Thanks
Stefano

Just a question.
Are you trying to directly compile the header file?
I am not sure what clang++ will do with plain .h files...
try adding a -x c++ to the arguments.

For the first time I built llvm + clang on ubuntu box for giving it a try.
I have this simple template function in a header file
template <typename T, typename U>
inline void SetMin(T &aVar,U aValue);
but clang++ returns
../src/myfile.h:13:1: error: unknown type name 'template'
template <typename T, typename U>
^
../src/myfile.h:13:10: error: expected identifier or '('
template <typename T, typename U>

Are you sure you're using clang++? I get the same error when I use
"clang -c test.h", but when I use "clang++ -c test.h" it compiles the
header just fine (though it does warn about treating a 'c-header' as
'c++-header' in C++ mode).

I tried to use also -std=gnu89 because I've read gcc (which compiles my
code) uses that standard by default, but with no success.

-std=gnu89 turns on GNU C mode, not GNU C++ mode. Try -std=c++98 or
-std=gnu++98 if you're using clang instead of clang++.

Sounds like you're trying to compile C++ code as C. Just because you invoke Clang as clang++ doesn't mean it runs in C++ mode, it only means it will link against the C++ runtime libraries. To actually compile in C++ mode, the file must have a C++ extension (.cxx, .cpp or .cc), or you have to override the language detection with the -x option.

Sebastian

It does, actually. From clang/tools/driver/driver.cpp:

  // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
  // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
  //
  // Note that we intentionally want to use argv[0] here, to support "clang++"
  // being a symlink.
  //
  // We use *argv instead of argv[0] to work around a bogus g++ warning.
  const char *progname = argv_[0];
  std::string ProgName(llvm::sys::Path(progname).getBasename());
  if (llvm::StringRef(ProgName).endswith("++") ||
      llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
    TheDriver.CCCIsCXX = true;
  }

That still doesn't mean that it will automatically use C++ as the input language. It might mean that it interprets .h files differently.

Sebastian

Hi,

I’m not compiling the header directly, it is included in another header, which is included in a *.cpp file. So, the extension should be interpreted as signaling a c++ source, right? What I did was basically

export CC=cflags
export CXX=cflags++
export CPP=cflags++

and then running the configure phase followed by a compilation phase. It really seems the C and not C++ mode is working here but I wonder why is that. Anyway, thanks a lot for the replies

Regards,
Stefano

update: I changed the flag -std to the value gnu++98 and now the things are working. So, using the -std=“gnu89” was forcing the C mode and not C++ mode. Sorry, I was not aware of that.

Regards,
Stefano