// Copyright (C) 2009-2010 Lorenzo Caminiti. // Use, modification, and distribution is subject to the // Contract++ Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt.) // Show workarounds to pass commas within macro parameters. #include #include #include template void print(const K& key, const T& element) { std::cout << key << " -> " << element << std::endl; } //[ commas_cpp template std::map fill(const std::map& source, const K& key, const T& element) CONTRACT_FUNCTION( (template)( (typename)(K) (typename)(T) ) // Commas within type expression using the macro. (typename CONTRACT_WRAP_TYPE( (std::map) )) (set)( // Or equivalently, not using the macro. (typename contract::wrap&) >::type)(source) (const K&)(key) (const T&)(element) ) (precondition) ({ // Commas within value expressions must be wrapped by `()`. CONTRACT_ASSERT( (std::map().empty()) ); }) (body) ({ // Commas within code blocks use same workarounds as above // wrapping differently commas within type or value expressions. // Or better, separate body definition so it is outside the macro. // OK, commas already wrapped by function call `()`. print(key, element); // Commas within type expression wrapped using the macro. typename CONTRACT_WRAP_TYPE((std::map)) m = source; // OK, commas already wrapped by if-statement `()`. if (0 == std::map().empty()) m[key] = element; return m; }) ) //] int main() { std::map m1; std::map m2 = set(m1, 1, 2.3); return 0; }