You mentioned having a build system issue that prompted this question. I once solved a similar problem by using comments in the source file. I used a Makefile rule like:
%.o: %.c
$(CC) $(CFLAGS) `if head -1 $< | grep -q 'C++'; then echo '-x c++'; else echo '-x c'; fi` -c -o $@ $<
Now if a source file began with a comment line like /* C++ */
it would be compiled as C++ and otherwise it would be compiled as C (regardless of whether CC=gcc
or CC=g++
)