1

In the following code:

   template< typename T, typename ValueType>
   bool SomeFunction(
        SomeWrapper<T> volatile& ioTarget,
        ValueType inCompare,
        ValueType inNewValue)
    {
         BOOST_STATIC_ASSERT(sizeof(SomeWrapper<T>) == sizeof(int));
    }

Boost Static Assert is failing with a compile time error.

Getting sizeof(T) has helped me get the size at the compile time as warning.

On analysing the Compiler Message , It's not pin-pointing to the actual source code location from where this Templatized Call has been made.

Is there any such other templatized technique which I can use for getting information about the actual source code file and line from where this Function is called.

PS: I had a thought about __FILE__ , but this being a macro is not helpful at compile time.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ATul Singh
  • 490
  • 3
  • 15

2 Answers2

0

You can make your code fail in some other way that makes the compiler output the information you need. This could e.g. be by doing some incomplete template instantiation in case of a failed assertion. As and example, compiling the following code:

template<bool>
struct tester;

template<>
struct tester<true> { }; // only define template for 'true'

template<class T>
void func(T t)
{
   tester<sizeof(T) == sizeof(int)>();
}

int main()
{
   int i;
   func(i);   // <- this call is ok :)
   double d;
   func(d);   // <- this is line 18, the offending call :(
   return 0;
}

Gives me the following output, when compiling with gcc:

g++-4.9 -O3 -fexpensive-optimizations -Drestrict= -std=c++11 -o main main.cpp  -pthread
main.cpp: In instantiation of ‘void func(T) [with T = double]’:
main.cpp:18:10:   required from here
main.cpp:10:4: error: invalid use of incomplete type ‘struct tester<false>’
    tester<sizeof(T) == sizeof(int)>();
    ^
main.cpp:2:8: error: declaration of ‘struct tester<false>’
 struct tester;
        ^

So gcc now tells me that the function call was made from line 18 in my main.cpp file, correctly identifying the offending line. This should be able to give the information you need.

EDIT 15/12-15: To print a compile-time warning you need to trigger a soft error, which does not result in a compiler error. This could e.g. be an overflow warning. To keep it short, the code would look something like this:

///
// struct that will overflow and print integer s at compile-time
////
template<unsigned s>
struct overflow
{
   operator char() { return (s + 256); }
};

/////
// function that will print warning message at compile-time
// warning message must be made as a class/struct
////
template<class MESSAGE>
void print_warning(MESSAGE)
{
   char(overflow<sizeof(MESSAGE)>());
};

struct this_is_a_warning // warning message
{
};

template<class T>
void func()
{
   print_warning(this_is_a_warning()); // <- this will print a warning, line 27
}

int main()
{
   func<double>(); // <- line 32
   return 0;
}

Compiling this with gcc gives me:

g++-4.9 -O3 -fexpensive-optimizations -Drestrict= -std=c++11 -o main main.cpp  -pthread main.cpp: In instantiation of ‘overflow<s>::operator char() [with unsigned int s = 1u]’: 
main.cpp:17:4:   required from ‘void print_warning(MESSAGE) [with MESSAGE = this_is_a_warning]’ 
main.cpp:27:37:   required from ‘void func() [with T = double]’
main.cpp:32:17:   required from here 
main.cpp:7:37: warning: overflow in implicit constant conversion [-Woverflow]
    operator char() { return (s + 256); }

'Clearly' showing showing the function call trace, ending with line 32 of main.cpp.

Banan
  • 434
  • 6
  • 12
0

I was working on XCode 6.3 LLVM , and it doesn't give me the exact line number from where it fails . But on analysing the Compiler Logs , It tells this information after an error was reported.

So just Looking at the logs after the error solved this . Now i am able to locate this instantiation.

But still this is an open question , If that template is succesfully instantiated, Is there any metaprogramming technique which can tell in some sort of warning ( not error ) that this template is instantiated from X file and Y Line No.

ATul Singh
  • 490
  • 3
  • 15
  • I kinda answered the original question... Added a section on how to get compile-time warnings as well. – Banan Dec 15 '15 at 14:23