constexpr-function参数如果直接使用则被视为constexpr,但如果用于调用另一个constexpr函数则不被认为是constexpr

时间:2014-08-23 18:57:47

标签: c++ constexpr

在尝试使用constexpr函数和模板(以及非类型模板参数)时,我偶然发现了一个现象,我无法理解哪条规则使它生效。

所以我的问题基本上是"为什么会发生这种情况",根据有关constexpr-s的规则。 "这"如下。

在其中一个constexpr函数中,如果直接使用参数,则在编译时计算中使用此参数没有问题。 (例如第2行)

当相同的参数用作另一个constexpr函数的参数时,编译器会抱怨此表达式(参数id)不是constexpr。 (例如第3行)

简而言之:

template <typename T> constexpr std::size size (T obj) { return obj.size(); }
template <typename T> constexpr auto sz1 (T obj) { return std::make_index_sequence< obj.size() > { }.size(); } // OK ...
template <typename T> constexpr auto sz2 (T obj) { return std::make_index_sequence< size(obj) > { }.size(); } // ERROR
  // "obj" is [suddenly] not a constexpr

g ++ - 4.9.1和clang ++ - 3.4.2都会发生这种情况。

以下是一个小型测试程序,用于快速简便的实验。


#include <utility>
#include <array>
#include <iostream>

// utils
template <size_t N> using require_constexpr = std::make_index_sequence<N>;
template <typename...> constexpr void noop (void) { }

// size() wrappers
template <typename T> constexpr std::size_t size (T obj) { return obj.size(); }
template <typename T> constexpr auto sz1 (T obj) { return size(require_constexpr< obj.size() > { }); }
template <typename T> constexpr auto sz2 (T obj) { return size(require_constexpr< size(obj) > { }); }

int main0 (int, char**)
{
  constexpr auto const ar = std::array<int, 4u> { 4, 5, 6, 7 };

  // Check constexpr-computability of size(), sz1() and the expansion of sz2()
  noop<
    require_constexpr<
      size(require_constexpr< ar.size() > { }) + sz1(ar) +
      size(require_constexpr< size(ar)  > { })
    >
  >();

  // But this is an error
  // ERROR: "obj" is not a constexpr in sz2()
//noop< require_constexpr< sz2(ar) > >();

  return 0;
}

编辑以下是相对编译输出。

 src/main1.cpp:12:87: error: non-type template argument is not a constant expression
     template <typename T> constexpr auto sz2 (T obj) { return size(require_constexpr< size(obj) > { }); }
                                                                                       ^~~~~~~~~
 src/main1.cpp:28:32: note: in instantiation of function template specialization 'sz2<std::array<int, 4> >' requested here
       noop< require_constexpr< sz2(ar) > >();
                                ^
 src/main1.cpp:12:92: note: read of non-constexpr variable 'obj' is not allowed in a constant expression
     template <typename T> constexpr auto sz2 (T obj) { return size(require_constexpr< size(obj) > { }); }
                                                                                            ^
 src/main1.cpp:12:92: note: in call to 'array(obj)'
 src/main1.cpp:12:49: note: declared here
     template <typename T> constexpr auto sz2 (T obj) { return size(require_constexpr< size(obj) > { }); }
                                            ^

GCC

src/main1.cpp: In substitution of ‘template<long unsigned int N> using require_constexpr = std::make_index_sequence<N> [with long unsigned int N = size<std::array<int, 4ul> >(obj)]’:
src/main1.cpp:12:102:   required from ‘constexpr auto sz2(T) [with T = std::array<int, 4ul>]’
src/main1.cpp:28:38:   required from here
src/main1.cpp:12:102: error: ‘obj’ is not a constant expression
     template <typename T> constexpr auto sz2 (T obj) { return size(require_constexpr< size(obj) > { }); }
                                                                                                      ^
src/main1.cpp:12:102: note: in template argument for type ‘long unsigned int’ 

2 个答案:

答案 0 :(得分:5)

这看起来像两个编译器如何处理编译器生成的拷贝构造函数的错误。

此代码使用clangg++编译:

#include <utility>

// utils
template <std::size_t N> struct require_constexpr { constexpr std::size_t size() const { return N; } };
struct test { 
  constexpr std::size_t size() const { return 0; } 
  constexpr test() { }
  constexpr test(const test &) { }
};
template <typename...> constexpr void noop (void) { }

// size() wrappers
template <typename T> constexpr std::size_t size (T obj) { return obj.size(); }
template <typename T> constexpr auto sz1 (T obj) { return size(require_constexpr< obj.size() > { }); }
template <typename T> constexpr auto sz2 (T obj) { return size(require_constexpr< size(obj) > { }); }

int main (int, char**)
{
  constexpr auto const ar = test();

  // Check constexpr-computability of size(), sz1() and the expansion of sz2()
  noop<
    require_constexpr<
      size(require_constexpr< ar.size() > { }) + sz1(ar) +
      size(require_constexpr< size(ar)  > { })
    >
  >();

  noop< require_constexpr< sz2(ar) > >();

  return 0;
}

但是如果我们改变那条线

constexpr test(const test &) { }

constexpr test(const test &) = default;

然后它既不编译(g++clang),即使两个构造函数之间完全没有区别(test是一个完全空的类),§ 12.8 [class.copy] / p13表明

  

如果隐式定义的构造函数满足要求   一个constexpr构造函数(7.1.5),隐式定义的   构造函数是constexpr

此外,如果隐式复制构造函数不是constexpr,那么带有constexpr的显式默认声明应该导致程序格式错误,需要诊断(§8.4。 2 [dcl.fct.def.default] / p2):

  

只有在明确默认的函数可以声明为constexpr   它会被隐含地声明为constexpr

但是如果第二个noop调用被注释掉,两个编译器(clangg++)都会编译第二个版本的代码。

答案 1 :(得分:5)

sz1和sz2之间的关键区别在于sz1将obj的地址传递给size成员函数,这不是常量表达式的有效结果,但可以作为中间结果操作数。 sz2对obj执行lvalue-&gt; rvalue转换以传递给size函数,并且因为obj不是常量,所以这使得表达式不是常量。

T.C.关于隐式与显式构造函数的观点很有意思。差异的来源是隐式普通拷贝构造函数执行按位拷贝,这涉及复制(非常量)填充字节,而用户提供的拷贝构造函数不复制任何东西。但是标准说隐式构造函数执行成员复制,所以它们应该被视为相同。

不清楚的是,他们是应该被拒绝还是被接受;严格阅读5.19表明两者都应该被拒绝,因为两者都涉及使用复制构造函数对obj进行左值 - >右值转换。我已经向C ++委员会提出了这个问题。

相关问题