c++ automatic runtime generation of constant

Started by
0 comments, last by Spoonbender 15 years, 8 months ago
I want to set SVN::Tag without having to call MakeTag myself in my program. I just want to include svn.h and have SVN::Tag already populated. Additionally, I want to be able to 'touch' a single file (svn.cpp) to force the pre-build step to fill in the true constants. Obviously this isn't valid C++ (static order initialization and its not syntactically correct either) but it demonstrates my intent. Am I stuck making this a singleton class? I don't like it because then my use case isn't as simple as reading a constant.
#pragma once
#include <string>

namespace SVN {

	const static std::string Revision = "$WCREV$";
	const static std::string Modified = "$WCMODS?Modified:$";
	const static std::string Date     = "$WCDATE$";
	const static std::string Range    = "$WCRANGE$";
	const static std::string Mixed    = "$WCMIXED?Mixed revision WC:$";
	const static std::string URL      = "$WCURL$";
	const static std::string Tag = MakeTag();

}

void MakeTag()
{
	size_t tag_pos = 1 + SVN::URL.find_last_of('/');
	SVN::Tag = SVN::URL.substr(tag_pos);
}



Advertisement
What would be the problem with the obvious solution?
#pragma once#include <string>namespace SVN {	std::string Revision() { return "$WCREV$"; }	std::string Modified() { return "$WCMODS?Modified:$"; }	std::string Date()     { return "$WCDATE$"; }	std::string Range()    { return "$WCRANGE$"; }	std::string Mixed()    { return "$WCMIXED?Mixed revision WC:$"; }	std::string URL()      { return "$WCURL$"; }	std::string Tag(){            size_t tag_pos = 1 + SVN::URL.find_last_of('/');	    return SVN::URL.substr(tag_pos);        }}


If you really really want a compile-time constant, you might be able to get boost.spirit to fill it in for you. But it seems like overkill when the above should work.

This topic is closed to new replies.

Advertisement