我知道sizeof...(Args...)在C++ 0x压缩模板参数列表中产生了类型的数量,但是我想根据其他功能实现它以用于演示目的,但它不会编译.
// This is not a solution -- overload ambiguity.
// template <typename... Args> size_t num_args (); // Line 7
// template <>
constexpr size_t num_args ()
{
return 0;
}
template <typename H, typename... T>
constexpr size_t num_args () // Line 16
{
return 1 + num_args <T...> (); // *HERE*
}
int main ()
{
std :: cout << num_args <int, int, int> ();
}
Run Code Online (Sandbox Code Playgroud)
这个错误*HERE*与
No matching function call to ...
... …Run Code Online (Sandbox Code Playgroud) 这个以前回答问题,解释了为什么我已张贴下面的代码不起作用.我有一个后续问题:是否存在概念上等效的解决方法,即实现编译时字符串连接,但是以C++ 11实际支持的方式实现?使用std :: string是完全不必要的.
constexpr std::string foo() { return std::string("foo"); }
constexpr std::string bar() { return std::string("bar"); }
constexpr std::string foobar() { return foo() + bar(); }
Run Code Online (Sandbox Code Playgroud) Clang接受此代码,但GCC拒绝它:
class Foo {
public:
static constexpr double kVal = 0.25f;
};
const double Foo::kVal;
Run Code Online (Sandbox Code Playgroud)
(使用clang 3.0和g ++ 4.6.3)
~$ clang++ foo.cc -std=c++11 -c
[ok]
~$ g++ foo.cc -std=c++0x -c
foo.cc:6:19: error: redeclaration ‘Foo::kVal’ differs in ‘constexpr’
foo.cc:3:34: error: from previous declaration ‘Foo::kVal’
foo.cc:6:19: error: declaration of ‘constexpr const double Foo::kVal’ outside of class is not definition [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
哪种解释是正确的?
我有以下代码
#include <iostream>
template <class T>
class A
{
public:
static constexpr int arr[5] = {1,2,3,4,5};
};
template<> constexpr int A<int>::arr[5];
int main()
{
A<int> a;
std::cout << a.arr[0] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译通过很好,但我有一个链接错误,我不明白
g++ -std=c++11 test.cpp -o test
/tmp/ccFL19bt.o: In function `main':
test01.cpp:(.text+0xa): undefined reference to `A<int>::arr'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 我决定给出一个旋转的新C++14定义,constexpr为了充分利用它,我决定编写一个小编译时字符串解析器.但是,我正在努力保持我的对象一段constexpr时间传递给一个函数.请考虑以下代码:
#include <cstddef>
#include <stdexcept>
class str_const {
const char * const p_;
const std::size_t sz_;
public:
template <std::size_t N>
constexpr str_const( const char( & a )[ N ] )
: p_( a ), sz_( N - 1 ) {}
constexpr char operator[]( std::size_t n ) const {
return n < sz_ ? p_[ n ] : throw std::out_of_range( "" );
}
constexpr std::size_t size() const { return sz_; }
};
constexpr long int …Run Code Online (Sandbox Code Playgroud) 这项工作:
template<typename T> struct Something
{ static constexpr const char* str = "int"; };
int main()
{ std::cout << Something<int>::str << std::endl; }
Run Code Online (Sandbox Code Playgroud)
但它没有:
template<typename T> struct Something
{ static constexpr const char str[] = "int"; };
int main()
{ std::cout << Something<int>::str << std::endl; }
Run Code Online (Sandbox Code Playgroud)
gcc-4.8说:"未定义参考Something<int>::str".
可以解决此错误,定义类外的静态成员:
template<typename T>
constexpr const char Something<T>::name[];
Run Code Online (Sandbox Code Playgroud)
为什么它不是指针而是数组?static constexpr毕竟两者都是会员.
我正在尝试sine使用泰勒系列扩展构建一个简单的函数,可以在编译时使用C++ 14进行评估constexpr.我的代码正在编译,但编译器不会生成常量.
sine 定义如下:
template <int P, typename T = double> constexpr T sine(T x) {
T result = x;
for (int i = 1; i < P; ++i)
result += power<T>(-1, i) * power<T>(x, 1 + 2 * i) / factorial<T>(1 + 2 * i);
return result;
}
Run Code Online (Sandbox Code Playgroud)
我可以提供代码power,factorial如果需要的话.它们是微不足道的constexpr.
我sine在这样的循环中调用:
template <int N> void test(double *out) {
for (int i = 0; i < N; ++i) { …Run Code Online (Sandbox Code Playgroud) 我遇到了一个看似违反直觉的错误,即无法将constexpr函数的值赋给constexpr文字(希望我正在使用正确的语言).这是一个例子:
class MyClass {
public:
static constexpr int FooValue(int n) { return n + 5; }
static constexpr int Foo5 = FooValue(5); // compiler error
static constexpr int Foo5Alt(void) { return FooValue(5); } // OK
};
Run Code Online (Sandbox Code Playgroud)
在GCC 4.8.4中,Foo5标记为field initializer is not constant.发现这个帖子暗示旧版本的GCC可能是罪魁祸首.所以我把它插入Coliru(GCC 6.2.0)并得到了错误'static constexpr int MyClass::FooValue(int)' called in a constant expression before its definition is complete.我添加Foo5Alt()了将其值作为constexpr函数返回而不是文字,并且编译得很好.
我想我不遵循为什么FooValue(5)不能用作初始化器Foo5.定义FooValue(int …
我想创建一个类,其方法类似于std::map,但应在编译时进行排序。哪些constexpr容器适合存储键template<class K>和值template<class V>?
std :: vector不满足这些要求。
UPD:我们发现std::array有很多constexpr方法。这足以解决我的问题std::array<std::pair<K, V> >。但是问题仍然存在。
假设我有一个由引擎参数化的struct模板S:
template<class Engine> struct S;
Run Code Online (Sandbox Code Playgroud)
我有两个引擎:具有constexpr成员函数的“静态”引擎size()和具有非constexpr成员函数的“动态” 引擎size():
struct Static_engine {
static constexpr std::size_t size() {
return 11;
}
};
struct Dynamic_engine {
std::size_t size() const {
return size_;
}
std::size_t size_ = 22;
};
Run Code Online (Sandbox Code Playgroud)
我想定义size()的成员函数S可以被用作constexpr如果发动机是size()是constexpr。我写:
template<class Engine>
struct S {
constexpr std::size_t size() const {
return engine_.size();
}
Engine engine_;
};
Run Code Online (Sandbox Code Playgroud)
然后,以下代码将与GCC,Clang,MSVC和ICC一起编译:
S<Static_engine> sta; // not constexpr
S<Dynamic_engine> …Run Code Online (Sandbox Code Playgroud)