C++ 带有外部静态表的constexpr函数

C++ 带有外部静态表的constexpr函数,c++,c++11,constexpr,one-definition-rule,C++,C++11,Constexpr,One Definition Rule,也有类似的问题,但我发现没有一个能直接回答这个问题 我想实现一个constexpr函数,如下所示: constexpr int Foo(int x) { static const int table[128] = { 3, 1, 4, 1, 5, ..., 99 }; return (0 <= x && x < 128) ? table[x] : 42; } 这是不允许的,因为你不能只声明constexpr,你必须定义它。但是如果我在头文件中定义表,我将违反

也有类似的问题,但我发现没有一个能直接回答这个问题

我想实现一个constexpr函数,如下所示:

constexpr int Foo(int x) {
  static const int table[128] = { 3, 1, 4, 1, 5, ..., 99 };
  return (0 <= x && x < 128) ? table[x] : 42;
}
这是不允许的,因为你不能只声明constexpr,你必须定义它。但是如果我在头文件中定义表,我将违反一个定义规则,对吗

constexpr int table[128] = { 3, 1, 4, 1, 5, ..., 99 };
constexpr int Foo(int x) {
  return (0 <= x && x < 128) ? table[x] : 42;
}
constexpr int table[128]={3,1,4,1,5,…,99};
constexpr int Foo(int x){
return(0不确定Q1(如果您定义
table
?),但对于Q2,我建议将
Foo()
作为
friend
函数,用于
table
私有
静态constepr
成员的类

以身作则

#include <iostream>

class wrapTable
 {
   private:
      static constexpr int table[] { 2, 3, 5, 7, 11, 13, 17, 19 };
      static constexpr int size { sizeof(table)/sizeof(table[0]) };

      friend constexpr int foo (int);
 };

constexpr int wrapTable::table[];

constexpr int foo (int x)
 { return (0 <= x && x < wrapTable::size) ? wrapTable::table[x] : 42; }

int main()
 {
   std::cout << "foo(3): " << foo(3) << std::endl; // print 7
   std::cout << "foo(9): " << foo(9) << std::endl; // print 42

   // compilation error: 'table' is a private member of 'wrapTable'
   // std::cout << "table(3): " << wrapTable::table[3] << std::endl;
 }
#包括
类可包装
{
私人:
静态constexpr int表[]{2,3,5,7,11,13,17,19};
静态constexpr int size{sizeof(table)/sizeof(table[0]);
友人康斯特普林特富(林特);
};
constexpr int wrapTable::表[];
constexpr int foo(int x)

{return(0)“我很小心使表函数如图所示是静态的,因为……”这对于
int
s来说根本不是一个现实的问题,因为。@ildjarn:这很公平,但这个问题还有其他动机。我的编译器(VC++2015)请不要让我在constexpr函数中声明该表为静态函数,我认为这需要遵守C++14。幸运的是,VC++2017将至少:“-]关于Q1,
constexpr
暗示
const
暗示
static
,所以是的,这是ODR冲突。在C++17中,可以标记
inline
来解决这个问题。Int我的编译器不接受
constexpr int wrapTable::table[]
行。即使它工作了,它仍然会污染每个消费者的名称空间,只是用
可包装的
而不是
@AdrianMcCarthy-你在使用哪个编译器?@AdrianMcCarthy-这比我的g++4.9.2或clang++3.5.0更新…我的代码错了?哪个消息错误显示了你的VC++?@AdrianMcCarthy
static constexpr int table[]{2,3,5,7,11,13,17,19};
to
static constexpr int table[8]{2,3,5,7,11,13,17,19};
在VC++2015中应该可以很好地编译。
constexpr int table[128] = { 3, 1, 4, 1, 5, ..., 99 };
constexpr int Foo(int x) {
  return (0 <= x && x < 128) ? table[x] : 42;
}
#include <iostream>

class wrapTable
 {
   private:
      static constexpr int table[] { 2, 3, 5, 7, 11, 13, 17, 19 };
      static constexpr int size { sizeof(table)/sizeof(table[0]) };

      friend constexpr int foo (int);
 };

constexpr int wrapTable::table[];

constexpr int foo (int x)
 { return (0 <= x && x < wrapTable::size) ? wrapTable::table[x] : 42; }

int main()
 {
   std::cout << "foo(3): " << foo(3) << std::endl; // print 7
   std::cout << "foo(9): " << foo(9) << std::endl; // print 42

   // compilation error: 'table' is a private member of 'wrapTable'
   // std::cout << "table(3): " << wrapTable::table[3] << std::endl;
 }