I'd like to ask if is it portable to rely on string literal address across translation units? I.e:
A given file foo.c
has a reference to a string literal "I'm a literal!"
, is it correct and portable to rely that in other given file, bar.c
in instance, that the same string literal "I'm a literal!"
will have the same memory address? Considering that each file will be translated to a individual .o
file.
For better illustration, follows an …
能够将传递给文字运算符的字符串转换为MPL序列将非常有用,因为我们可以根据字符串的内容控制代码生成.以前,我认为这是不可能的,因为constexpr函数的参数不被视为函数体内的常量表达式.但是,我提出了在Clang 3.4.2和GCC 4.8.2下编译的以下解决方法:
#include <cstdint>
#include <iostream>
#include <typeinfo>
struct string
{
const uintmax_t m_str[64];
const size_t m_length;
template <class... Ts>
constexpr string(const Ts... ts) :
m_str{(uintmax_t)ts...}, m_length{sizeof...(Ts)} {}
constexpr size_t size() const { return m_length; }
constexpr size_t length() const { return m_length; }
constexpr uintmax_t operator[](size_t n) const { return m_str[n]; }
};
template <uintmax_t... Ts> struct sequence {};
constexpr auto
operator"" _tag(const char* str, size_t n)
{
return n == 0 ? string{} :
n == 1 …
Run Code Online (Sandbox Code Playgroud) c++ template-meta-programming user-defined-literals constexpr c++14
#include <iostream>
#include <string>
constexpr std::string appendStringC(std::string s)
{
return s + " Constexpr func";
}
std::string appendString(std::string s)
{
return s + " Regular func";
}
int main()
{
std::string s1 = "String 1";
std::string s2 = "String 2";
std::cout << std::endl
<< appendStringC(s1) << std::endl
<< appendString(s2) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用constexpr执行编译时字符串前缀/后缀连接操作.但是,此示例产生以下错误:
const_string_generation.cpp: In function ‘constexpr std::__cxx11::string appendStringC(std::__cxx11::string)’:
const_string_generation.cpp:4:23: error: invalid type for parameter 1 of constexpr function ‘constexpr std::__cxx11::string appendStringC(std::__cxx11::string)’
constexpr std::string appendStringC(std::string s)
^
In …
Run Code Online (Sandbox Code Playgroud) 我想做的是创建:
template<Args... args)>
int println(Args...) {
// implementation which calls:
// printf("<string literal format string at compile time>", args...);
// additional perk would be compile time type checking
// I expect to provide a format string for each type by some template
// specialization.
}
Run Code Online (Sandbox Code Playgroud)
我一直在用编译时字符串文字分析两个有趣的工作:
编译时间内存对齐的字符串文字
100%constexpr字符串实现
http://sourceforge.net/p/constexprstr/code/HEAD/tree/no-pp-constexpr_string.cpp
基本上我或多或少能够静态地推断格式所需的字符串文字的长度,但仅仅是编译器拒绝将我的工作视为constexpr.另一个大问题是,当使用上述链接中的constexpr字符串时,编译器永远无法推断出结果字符串的大小.
我越努力实现这一点,我的无知就越多,超过了我的热情.我将不胜感激任何解决部分或全部问题的技巧和/或代码示例.
注意:我不是在寻找有关使用不同形式的日志记录的建议,例如通过cout.
注意2:不应该使用任何std :: string,因为它们是运行时
注3:通常所有类型安全的printfs都使用不同的方法,我很熟悉这可以通过多次调用轻松完成printf
.我想一次性完成.我也知道缓冲区可以逐步构建,但这个问题是关于构造格式字符串.:)
更新:基本上代码的哪个部分需要实现
constexpr const char* formatString = build_compile_time_format_string(args...);
// build_compile_time_format_string(3, "hi", -3.4)
// should evaluate to "%d %s %f"
// or to …
Run Code Online (Sandbox Code Playgroud) 我想从 char* 和“kernel32.dll”中得到积分常数,但总是失败。以下是我失败的尝试,谁能告诉我正确的用法?
error 1: cout << std::integral_constant<const char*, "kernel32.dll">::value << endl;
error 2: cout << std::integral_constant<char*, "kernel32.dll">::value << endl;
error 3: cout << std::integral_constant<char[], "kernel32.dll">::value << endl;
error 4: cout << cout << std::integral_constant<char*, static_cast<char*>("kernel32.dll")>::value << endl;
Run Code Online (Sandbox Code Playgroud)
以上 4 条语句具有相同的错误信息。:
Console.cpp(181): error C2762: 'std::integral_constant' : invalid expression as a template argument for '_Val'
1> D:\Programfiles\Visual Studio 2013\VC\include\xtr1common(35) : see declaration of 'std::integral_constant'
1>Console.cpp(181): error C2955: 'std::integral_constant' : use of class template requires template argument list
1> D:\Programfiles\Visual Studio 2013\VC\include\xtr1common(35) …
Run Code Online (Sandbox Code Playgroud)