Dependent Origination

C++: Want a string literal as a template parameter?

Posted on: June 2, 2012

You’ve come to the right place!

template<typename T, string& url, int port> void your_function(param1_type param1 ….);

Then declare your strings in the global scope:

string prod_url = “your string literal”;

then instantiate:

your_function<T, prod_url, 443>(param1, param2, param3,…);

The compiler will be happy!

Note:

1. no amount of const, constexpr, static can make the compiler happy so the literals have to be declared in the global scope — the compiler apparently just wants it to be there, to be there, to be there.

2. i couldn’t make const char* work (instead of a string&) as a template parameter type.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

June 2012
M T W T F S S
« May   Jul »
 123
45678910
11121314151617
18192021222324
252627282930  

Twitter

  • is having a nice feeling of connecting dots all over the place. they are now illuminating the same point. 1 day ago
  • Style Transfer is fun! TensorFlow rocks! #WTM17 https://t.co/zYP0IFIDfp 1 month ago
  • couldn't get over the jetlag, sleeping during the day from seven to four, for days. 3 months ago
  • is emptying trash and happily discovering the available disk space now ranks at 100G+. 4 months ago
  • is looking at other people's intentions, not their capabilities, and feeling much happier every day :) 1 year ago

Flickr Photos

IMG_3517

IMG_3515

IMG_3505

IMG_3497

IMG_3261

IMG_3260

IMG_3255

IMG_2736

IMG_2733

IMG_2629

More Photos
%d bloggers like this: