假设我有这样一个类:
class ApplicationDefs{
public static final String configOption1 = "some option";
public static final String configOption2 = "some other option";
public static final String configOption3 = "yet another option";
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序中的许多其他类都使用这些选项.现在,我想单独更改其中一个选项并仅部署已编译的类.但是,如果这些领域在消费者类中被列入,这就变得不可能了吗?
是否有任何选项可以禁用编译时常量的内嵌?
以下代码在C++ 11中是合法的.
template<int... N>
std::tuple<decltype(N)...> f()
{
return std::make_tuple(7 + N...);
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
c++ templates compile-time-constant variadic-templates c++11
我有以下代码,我得到错误"PHP致命错误:常量表达式包含无效操作".当我在构造函数中定义变量时,它工作正常.我正在使用Laravel框架.
<?php
namespace App;
class Amazon
{
protected $serviceURL = config('api.amazon.service_url');
public function __construct()
{
}
}
Run Code Online (Sandbox Code Playgroud) 我在C#winforms应用程序中设置了一些默认颜色,如下所示:
readonly Color ERROR = Color.Red;
readonly Color WARNING = Color.Orange;
readonly Color OK = Color.Green;
Run Code Online (Sandbox Code Playgroud)
据我所知,readonly对我来说基本上是一个常数.如果我尝试将它们定义为常量,编译器会指示它必须是编译时常量,而Color不是.
我很好地保留这些原样,还是有一些方法来定义我应该注意的这些常量?
(目的只是为了有一个位置来更改所有颜色以进行日志记录.)
假设我有类似的东西:
#define SIZE 32
/* ... */
unsigned x;
/* ... */
x %= SIZE;
Run Code Online (Sandbox Code Playgroud)
将在x % 32
通常被减少到x & 31
由最C/C++编译器,如GCC?
gcc constants modulo compile-time-constant compiler-optimization
我正在研究注释处理器.此代码编译:
package sand;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
@SupportedAnnotationTypes("sand.Foo")
public class FooProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false; // TODO
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我对字符串常量"sand.Foo"感到不满(在这种情况下不是太多,但对于将来会更多).
如果Foo
重命名或移动到另一个包,此代码仍将编译,但它不起作用.
我想做的事情如下:
@SupportedAnnotationTypes(Foo.class)
Run Code Online (Sandbox Code Playgroud)
这样,如果Foo的名称发生变化,编译将失败,有人必须更正文件.
但这不起作用,因为a Class
不是String
.所以我尝试过:
@SupportedAnnotationTypes(Foo.class.getName())
Run Code Online (Sandbox Code Playgroud)
但编译器并不认为这是一个常量表达式,这在此上下文中是必需的,因此也不起作用.
在编译时有没有办法将类文字强制转换为它的名字?
我有一个像
template <size_t N>
class A
{
template <size_t N>
someFunctions() {};
};
Run Code Online (Sandbox Code Playgroud)
现在我想创建类的实例并在 for 循环中为一组许多值调用其中的函数,例如
// in main()
int main()
{
for (int i = 1; i <= 100; i++)
{
const int N = i; // dont know how to do this
A<N> a;
a.functionCalls();
}
}
Run Code Online (Sandbox Code Playgroud)
这该怎么做?希望有一种方法可以做到这一点。
c++ templates for-loop compile-time-constant template-classes
我精通典型的范例:
//.h
extern const int myInt;
//.c, .m, .cpp, what have you
const int myInt = 55;
Run Code Online (Sandbox Code Playgroud)
但是必须有一种方法可以将其放入.h
文件中,以便与库或其他无法访问实现文件的实例一起使用.
例如,我正在尝试向Xcode项目中NSString
的.h
文件添加一个常量,如下所示:
static NSString *const myString = @"my_string";
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用时myString
,我得到了错误
Initializer元素不是编译时常量
on myString
,表示未正确实例化.如何在C++或Objecitve-C头文件中声明编译时常量?
c++ constants objective-c header-files compile-time-constant
是否可以即时声明一个新类型(一个空的struct或没有实现的struct)?
例如
constexpr auto make_new_type() -> ???;
using A = decltype(make_new_type());
using B = decltype(make_new_type());
using C = decltype(make_new_type());
static_assert(!std::is_same<A, B>::value, "");
static_assert(!std::is_same<B, C>::value, "");
static_assert(!std::is_same<A, C>::value, "");
Run Code Online (Sandbox Code Playgroud)
一个“手动”解决方案是
template <class> struct Tag;
using A = Tag<struct TagA>;
using B = Tag<struct TagB>;
using C = Tag<struct TagC>;
Run Code Online (Sandbox Code Playgroud)
甚至
struct A;
struct B;
struct C;
Run Code Online (Sandbox Code Playgroud)
但是对于模板/ meta,一些魔术make_new_type()
功能会很好。
有状态的元编程格式不正确,这样的事情是否可能呢?
c++ templates metaprogramming stateful compile-time-constant
下面的代码合法吗?
template <int N>
class foo {
public:
constexpr foo()
{
for (int i = 0; i < N; ++i) {
v_[i] = i;
}
}
private:
int v_[N];
};
constexpr foo<5> bar;
Run Code Online (Sandbox Code Playgroud)
Clang 接受它,但 GCC 和 MSVC 拒绝它。
GCC 的错误是:
main.cpp:15:18: error: 'constexpr foo<N>::foo() [with int N = 5]' called in a constant expression
15 | constexpr foo<5> bar;
| ^~~
main.cpp:4:15: note: 'constexpr foo<N>::foo() [with int N = 5]' is not usable as a 'constexpr' function because:
4 …
Run Code Online (Sandbox Code Playgroud) c++ compile-time-constant template-meta-programming constexpr
c++ ×5
constants ×3
templates ×3
java ×2
c# ×1
c++11 ×1
class ×1
colors ×1
constexpr ×1
for-loop ×1
gcc ×1
header-files ×1
inlining ×1
modulo ×1
objective-c ×1
php ×1
properties ×1
reflection ×1
stateful ×1
syntax-error ×1