Search code examples
c++c++11eos

Variable as C++11 attribute argument


Given an attribute that accepts a string argument, like [[deprecated("reason")]], is it possible to use anything else other than a hardcoded string literal?

In my case, I'm developing a smart contract for the EOS.IO blockchain which exposes the [[eosio::on_notify("account::action")]] attribute and I would like to extract the "account::action" part in a configuration file.

I understand that there is an EOS.IO specific Stack Exchange network but I think this question applies to all C++11 attributes.

Attempts so far

I tried defining these parameters as static const strings in a namespace in a config.hpp header:

// ...omitting irrelevant parts

namespace config {
    static const std::string test = "eosio.token::transfer";
}

and later importing the header and using the static string:

// contract.cpp

// ...omitting irrelevant parts

#include "config.hpp"

class [[eosio::contract]] myapp : public contract {
public:

    [[eosio::on_notify(config::test)]]
    void on_transfer();
};

However, the compiler complains:

error: 'on_notify' attribute requires a string [[eosio::on_notify(config::test)]]


Solution

  • No.

    Attributes are baked into your source code literally.

    You could use the preprocessor though, with help from your build system to define the desired macro.