0

I'm trying to pass a string as a C++ template parameter, but I can't seem to be able to get it to work. For the record, I'm working with the SystemC library (hence all the sc_xxx stuff). According to this answer, what I'm doing should work, but I cannot see what I'm doing wrong. The compiler is telling me that " filePath cannot appear in a constant" expression." Any help would be appreciated.

main.cpp

int sc_main(int argc, char* argv[])
{
  const char filePath[] = "test.txt";
  Interconnect<sc_uint<32>, filePath, 10> myInterconnect;


  return 0;
}

interconnect.h

template<class T, const char filePath[], unsigned nPortPairs = 10>
SC_MODULE(Interconnect)
{
public:
 ...
};
Community
  • 1
  • 1
audiFanatic
  • 2,296
  • 8
  • 40
  • 56

3 Answers3

2

Your filePath is a local automatic variable, and as such its address is a run-time feature. The template needs an address known at compile time. In C++03 there was also an issue about linkage; I'm not totally sure but I think the linkage issue was fixed in C++11.

A simple fix is to let the filepath be an ordinary constructor argument.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

I see two differences between what you are doing and the answer you provided a link to.

  1. Your string constant is a local variable. In the other answer it is a global variable.
  2. Your template parameter is a const char array. In the other answer it is a const char *.

My guess is that you simply need to eliminate these differences.

I found another resource that seems to confirm my guesses.

Benilda Key
  • 2,836
  • 1
  • 22
  • 34
0

If you have boost, this can be simplified with following macro:

#define C_STR(str_) boost::mpl::c_str< BOOST_METAPARSE_STRING(str_) >::value

Then use as follows:

template<const char* str>
struct testit{
};
testit<C_STR("hello")> ti;
darune
  • 10,480
  • 2
  • 24
  • 62