Questions tagged [function-attributes]

36 questions
39
votes
3 answers

pure/const function attributes in different compilers

pure is a function attribute which says that a function does not modify any global memory. const is a function attribute which says that a function does not read/modify any global memory. Given that information, the compiler can do some additional…
Albert
  • 65,406
  • 61
  • 242
  • 386
23
votes
3 answers

How to use GCC's printf format attribute with C++11 variadic templates?

I have a C++ class that is the frontend for a logging system. Its logging function is implemented using C++11's variadic templates: template void Frontend::log(const char *fmt, Args&&... args) { backend->true_log(fmt,…
Marco Leogrande
  • 8,050
  • 4
  • 30
  • 48
19
votes
3 answers

Decorator to set attribute of function

I want different functions to be executable only if the logged in user has the required permission level. To make my life more simple I want to use decorators. Below I attempt to set attribute permission on 'decorated' functions - as shown…
13
votes
2 answers

What does the unavailable attribute in Objective C mean?

What does the unavailable attribute in Objective C do? __attribute__((unavailable("message"))) Is there any online reference to this and other attributes in Clang?
cfischer
  • 24,452
  • 37
  • 131
  • 214
11
votes
3 answers

Best way to initialize variable in a module?

Let's say I need to write incoming data into a dataset on the cloud. When, where and if I will need the dataset in my code, depends on the data coming in. I only want to get a reference to the dataset once. What is the best way to achieve…
Robbe
  • 2,610
  • 1
  • 20
  • 31
10
votes
2 answers

function attribute returns_twice

I just was looking up funciton attributes for gcc (http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html) and came across the returns_twice attribute. And I am absolutely clueless in what case a function can return twice... I looked…
someone
  • 1,638
  • 3
  • 21
  • 36
7
votes
0 answers

GCC/Clang function attributes per template instantiation

I have some hand-vectorized C++ code that I'm trying to make a distribute-able binary for via function multiversioning. Since the code uses SIMD intrinsics for different instruction sets (SSE2, AVX2, AVX512), it uses template specializations to…
7
votes
2 answers

Reassign a function attribute makes it 'unreachable'

I have a simple little decorator, that caches results from function calls in a dict as a function attribute. from decorator import decorator def _dynamic_programming(f, *args, **kwargs): try: f.cache[args] except KeyError: …
hildensia
  • 1,650
  • 2
  • 12
  • 24
6
votes
1 answer

Inconsistent gcc behaviour for __attribute((const))

I've come accross a very strange behavior in gcc regarding operators and functions marked with __attribute((const)). Logical and arithmetic operators lead to different optimizations, and I don't understand why. It's not really a bug since…
Antoine
  • 13,494
  • 6
  • 40
  • 52
4
votes
0 answers

How to get custom attributes in LLVM Pass

I implemented a custom attribute in clang as described in the official manual: How to add an attribute in Clang I added a definition to include/clang/Basic/Attr.td def attr : InheritableAttr { let Spellings = [GCC<"attr">, Declspec<"attr">]; //…
leeB0Wen
  • 41
  • 1
3
votes
1 answer

Pure version of `std.format.format!`?

I'd like to convert a double to a string in a pure function. I'm confused as to why this isn't pure: wstring fromNumber(double n) pure { import std.format; return std.format.format!("%s"w)(n); } Is there a way to implement this function in…
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
3
votes
0 answers

A more standard __attribute__((warning("msg"))

In my C++ library, I have a function that is still there, 1) for debugging 2) for small operations. The function is basically a very slow fallback of more efficient versions. (think of a loop of individual assignments vs memcpy for example)> For…
alfC
  • 14,261
  • 4
  • 67
  • 118
3
votes
0 answers

State variables in Python: when to choose classes, nonlocal variables, or function attributes

There are (at least) three different ways to track state information for functions in python (examples presented without any meaningful way to use the state information, obviously): OOP Classes: class foo: def __init__(self, start): …
DrMag
  • 39
  • 1
  • 3
3
votes
1 answer

accessing function attributes inside decorators

Is it possible to access function attributes inside a decorator? Consider below piece of code. def deco(a): def wrap(): print(a.status) a() print(a.status) return wrap @deco def fun1(): …
3
votes
3 answers

Create a function-attribute of a function, which is, in its turn, a method of an object literal

It is known, that in JS you're allowed to create a function inside function, like this: function digit() { return 9 }; digit.five = function() { return 5 }; digit(); // 9 digit.five() // 5 I don't know whether it is a bad practice or not…
Parzh from Ukraine
  • 7,999
  • 3
  • 34
  • 65
1
2 3